摘要:
原题地址跟2Sum、3Sum、4Sum类似,都是:排序+搜索+剪枝令sum = num[i] + num[j] + num[k] + (-target)(将-target看做一个必选的数),那这道题就跟4Sum(参见这篇文章)几乎一样了,变成了寻找最接近0的和。需要剪枝的地方:1. 数字太小,肯定不... 阅读全文
posted @ 2015-01-30 20:32
李舜阳
阅读(205)
评论(0)
推荐(0)
摘要:
原题地址BFSWord Ladder II的简化版(参见这篇文章)由于只需要计算步数,所以简单许多。代码: 1 int ladderLength(string start, string end, unordered_set &dict) { 2 if (start == end) ... 阅读全文
posted @ 2015-01-30 18:39
李舜阳
阅读(154)
评论(0)
推荐(0)
摘要:
原题地址基本链表操作代码: 1 ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { 2 ListNode *h = NULL; 3 ListNode *t = NULL; 4 int carry ... 阅读全文
posted @ 2015-01-30 17:28
李舜阳
阅读(167)
评论(0)
推荐(0)
摘要:
原题地址标准DFS+剪枝。将数字提前排序,一来方便去重,二来创造剪枝条件。1. 如果当前数字过大,就算加上最小的数字最后都超过target,停止继续尝试之后的数字,因为后面的数字更大2. 如果当前数字过小,就算加上后面最大的数字最后都不到target,不用再考虑这个数字了,继续尝试后面更大的数字3.... 阅读全文
posted @ 2015-01-30 17:20
李舜阳
阅读(263)
评论(0)
推荐(0)
摘要:
原题地址简单DFS题目代码: 1 vector res; 2 3 void dfs(string &digits, vector &i2s, string ans, int pos) { 4 if (pos == digits.length()) { 5 res.push_ba... 阅读全文
posted @ 2015-01-30 15:42
李舜阳
阅读(141)
评论(0)
推荐(0)
摘要:
原题地址如果不限交易次数,把所有递增序列差值求和即可。代码: 1 int maxProfit(vector &prices) { 2 if (prices.empty()) 3 return 0; 4 5 int profi... 阅读全文
posted @ 2015-01-30 15:34
李舜阳
阅读(178)
评论(0)
推荐(0)
摘要:
原题地址Best Time to Buy and Sell Stock III(参见这篇文章)的简化版迭代法求最大利润。当然是在峰值卖出,在低谷买进利润最大了。所以从后向前遍历所有价格,如果当前价格比之前的峰值价格还高,更新峰值价格,否则就用峰值价格减去当前价格看看利润多少。代码: 1 int ma... 阅读全文
posted @ 2015-01-30 15:26
李舜阳
阅读(124)
评论(0)
推荐(0)
摘要:
原题地址数字三角,经典动归题。编码时可状态压缩成1维数组代码: 1 int minimumTotal(vector > &triangle) { 2 if (triangle.empty()) return -1; 3 4 vector layer(... 阅读全文
posted @ 2015-01-30 15:21
李舜阳
阅读(120)
评论(0)
推荐(0)
摘要:
原题地址最初的想法是用动态规划,令palin[i][j]表示s[i..j]是否是回文串,则有递推公式palin[i][j] = s[i] == s[j] && palin[i+1][j-1]。因为递推式只使用相邻层的值,所以编码的时候可以将二维状态数组压缩成一维的。代码: 1 string long... 阅读全文
posted @ 2015-01-30 15:02
李舜阳
阅读(212)
评论(0)
推荐(0)
摘要:
原题地址Populating Next Right Pointers in Each Node II(参见这篇文章)的简化版,代码不变代码: 1 void connect(TreeLinkNode *root) { 2 queue layer; 3 4 layer.push(root); ... 阅读全文
posted @ 2015-01-30 14:18
李舜阳
阅读(155)
评论(0)
推荐(0)
摘要:
原题地址计算循环小数先把负数转化成正数,然后计算,最后添加符号当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧,比Hard难度的题还低。最残暴的做法是直接转成64位长整型,比如下面的代码。好处是代码简洁了许多,不过这是... 阅读全文
posted @ 2015-01-30 13:06
李舜阳
阅读(354)
评论(0)
推荐(0)
摘要:
原题地址先序遍历二叉树,递归展开。别忘了将left设为NULL,如果忘掉的话可能报Runtime Error,而且这个RE很难查出原因。代码: 1 TreeNode *solve(TreeNode *root) { 2 if (!root) return NULL; 3 4 ... 阅读全文
posted @ 2015-01-30 10:54
李舜阳
阅读(163)
评论(0)
推荐(0)
摘要:
原题地址二叉树基本操作——遍历题目没说数字都是正数,所以没法剪枝,只能全部遍历一遍。代码: 1 vector > res; 2 3 void traverse(TreeNode *root, vector ans, int sum) { 4 if (!root) 5 retur... 阅读全文
posted @ 2015-01-30 10:30
李舜阳
阅读(136)
评论(0)
推荐(0)
摘要:
原题地址简化版本的Find Minimum in Rotated Sorted Array II(参见这篇文章)二分查找最小值,每次只需要查看其中一个二分区间即可。如果A[i] A[j]则说明A[i..j]肯定是非连续的,说明最小值肯定出现在A[i..j]中当中,之后继续在这一半内查找,另一半可以... 阅读全文
posted @ 2015-01-30 10:16
李舜阳
阅读(117)
评论(0)
推荐(0)
摘要:
原题地址简单动态规划,跟最大子串和类似。一维状态空间可以经过压缩变成常数空间。代码: 1 int maxProduct(int A[], int n) { 2 if (n = 0; i--) {10 int tmp = minp;11 ... 阅读全文
posted @ 2015-01-30 09:57
李舜阳
阅读(144)
评论(0)
推荐(0)
摘要:
原题地址跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了。代码: 1 TreeNode *buildBST(ListNode *head, int len) { 2 if (len next; 9 ... 阅读全文
posted @ 2015-01-30 09:48
李舜阳
阅读(148)
评论(0)
推荐(0)
摘要:
原题地址对于已排序数组,二分法递归构造BST代码: 1 TreeNode *buildBST(vector &num, int i, int j) { 2 if (i > j) 3 return NULL; 4 5 int m = (i + j) /2; 6 T... 阅读全文
posted @ 2015-01-30 09:31
李舜阳
阅读(503)
评论(0)
推荐(0)
摘要:
原题地址将单词按空格分词,然后倒序拼接即可代码: 1 void reverseWords(string &s) { 2 vector words; 3 4 int start = -1; 5 int len = 0; 6 ... 阅读全文
posted @ 2015-01-30 09:26
李舜阳
阅读(196)
评论(0)
推荐(0)
摘要:
原题地址二叉树基本操作[ ]O[ ][ ][ ]O代码: 1 TreeNode *restore(vector &inorder, vector &postorder, int ip, int pp, int len) { 2 if (len == 0) 3 ... 阅读全文
posted @ 2015-01-30 09:11
李舜阳
阅读(145)
评论(0)
推荐(0)
摘要:
原题地址基本二叉树操作。O[ ][ ][ ]O[ ]代码: 1 TreeNode *restore(vector &preorder, vector &inorder, int pp, int ip, int len) { 2 if (len left = r... 阅读全文
posted @ 2015-01-30 09:02
李舜阳
阅读(128)
评论(0)
推荐(0)

浙公网安备 33010602011771号