随笔分类 -  题库Leetcode简单题

https://leetcode.com/problemset/algorithms/中Easy级别的题
摘要:二叉树的层次遍历 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) 阅读全文
posted @ 2016-03-04 22:33 Breeze0806 阅读(361) 评论(0) 推荐(0)
摘要:给定一个数n 求出n!的末尾0的个数。 n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. 1 class Solution { 2 public: 3 int trailingZeroes(int n) { 4 int ans = 0; 阅读全文
posted @ 2016-03-04 22:26 Breeze0806 阅读(140) 评论(0) 推荐(0)
摘要:杨辉三角,即组合数 递推 1 class Solution { 2 vector<vector<int>> v; 3 public: 4 Solution() { 5 for(int i = 0; i < 50; ++i){ 6 vector<int> t(i+1,1); 7 for(int j = 阅读全文
posted @ 2016-03-03 20:49 Breeze0806 阅读(129) 评论(0) 推荐(0)
摘要:题意让大数加1 我的做法是先让个位+1,再倒置digits,然后进位,最后倒置digits,得到答案。 1 class Solution { 2 public: 3 vector<int> plusOne(vector<int> &digits) { 4 digits[digits.size() - 阅读全文
posted @ 2016-03-02 21:55 Breeze0806 阅读(139) 评论(0) 推荐(0)
摘要:题目本身是去重 由于我很懒,所以用了STL库里的unique函数来去重,小伙伴们可以考虑自己实现去重的函数,其实并不复杂。 1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 return unique 阅读全文
posted @ 2016-03-02 21:27 Breeze0806 阅读(151) 评论(0) 推荐(0)
摘要:和remove zero类似的方法完成该题 1 class Solution { 2 public: 3 int removeElement(vector<int>& nums, int val) { 4 vector<int>::size_type j = 0; 5 for(vector<int> 阅读全文
posted @ 2016-03-02 21:19 Breeze0806 阅读(123) 评论(0) 推荐(0)
摘要:题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } queue<Nodetype> q; q.push(firstnode); while(!q.empty( 阅读全文
posted @ 2016-03-01 20:23 Breeze0806 阅读(213) 评论(0) 推荐(0)
摘要:判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert Binary Tree,然后用Same Tree比较左右子树 而我的做法是改下Same Tree的函数 阅读全文
posted @ 2016-03-01 20:01 Breeze0806 阅读(149) 评论(0) 推荐(0)
摘要:题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考《动态规划经典教程》,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马时的最大价值是多少, 因此所有的dp[i] = max(dp[i-2]+nums[i],dp[i-1]) (其中 阅读全文
posted @ 2016-03-01 19:55 Breeze0806 阅读(352) 评论(0) 推荐(0)
摘要:判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 1 /** 2 * Definition for a binary tree node. 3 * struct Tre 阅读全文
posted @ 2016-03-01 19:10 Breeze0806 阅读(134) 评论(0) 推荐(0)
摘要:本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa有关,而pop(),peek()与sb有关,即将sa输入元素出栈放到sb中(函数move). 为此, 阅读全文
posted @ 2016-03-01 19:04 Breeze0806 阅读(173) 评论(0) 推荐(0)
摘要:合并两个已排序的链表,考到烂得不能再烂的经典题,但是很多人写这段代码会有这样或那样的问题 这里我给出了我的C++算法实现 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListN 阅读全文
posted @ 2016-03-01 16:36 Breeze0806 阅读(162) 评论(0) 推荐(0)
摘要:同样是判断数是否是2的n次幂,同 Power of three 1 class Solution { 2 public: 3 bool isPowerOfTwo(int n) { 4 return (n > 0) && (((long long)1<<31) % n == 0); 5 } 6 }; 阅读全文
posted @ 2016-03-01 16:24 Breeze0806 阅读(157) 评论(0) 推荐(0)
摘要:判断一个数是否是3的n次幂 这里我用了一点巧,所有的int范围的3的n次幂是int范围最大的3的n次幂数(即3^((int)log3(MAXINT)) = 1162261467)的约数 这种方法是我目前觉得是最好的,不容易出错,其他的方法因为精度问题而很容易错。 1 class Solution { 阅读全文
posted @ 2016-03-01 16:21 Breeze0806 阅读(742) 评论(0) 推荐(0)
摘要:Ugly Number的质因数仅为2,3,5 将输入的数分别除以2,3,5直到不能除,看是否为1,为1的是Ugly Number,其他则不是。 1 class Solution { 2 public: 3 bool isUgly(int num) { 4 if(num<=0) return fals 阅读全文
posted @ 2016-03-01 16:12 Breeze0806 阅读(784) 评论(0) 推荐(0)
摘要:其实就是斐波那契数列 参考dp[n] = dp[n-1] +dp[n-2]; 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 int f1 = 0; 5 int f2 = 1; 6 int f3 = 0; 7 for(int i = 阅读全文
posted @ 2016-02-24 10:40 Breeze0806 阅读(138) 评论(0) 推荐(0)
摘要:就是将链表中的重复元素去除 我的方法很简单就是如果链表的前后元素相同的话,将后一个元素删除 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * 阅读全文
posted @ 2016-02-24 10:36 Breeze0806 阅读(129) 评论(0) 推荐(0)
摘要:Given 1->2->3->4->5->NULL, return 1->3->5->2->4->NULL. 就是将序号为单数的放在前面,而序号为偶数的放在后面 我的方法是讲序号为偶数的的插入到链表末尾。 1 /** 2 * Definition for singly-linked list. 3 阅读全文
posted @ 2016-02-24 10:28 Breeze0806 阅读(139) 评论(0) 推荐(0)
摘要:将单向链表反转完成如图操作,依次进行即可123 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListN... 阅读全文
posted @ 2016-01-25 22:40 Breeze0806 阅读(175) 评论(0) 推荐(0)
摘要:题意:将罗马数字1到3999转化成自然数字,这里用了STL库map将罗马字符映射到自然数字。I,V,X,L,C,D,M -> 1,5,10,50,100,500,1000m[s[i]] m; 4 Solution(){ 5 const int N = 7; 6 ... 阅读全文
posted @ 2016-01-25 22:17 Breeze0806 阅读(165) 评论(0) 推荐(0)