摘要:
原题地址找规律+模拟,真没啥可说的。代码: 1 string intToRoman(int num) { 2 string res; 3 4 while (num >= 1000) { 5 res += "M"; 6 ... 阅读全文
posted @ 2015-01-27 19:42
李舜阳
阅读(152)
评论(0)
推荐(0)
摘要:
原题地址基本动态规划题代码: 1 int minPathSum(vector > &grid) { 2 if (grid.empty() || grid[0].empty()) return 0; 3 4 int m = grid.size(); 5 int n = grid[0].s... 阅读全文
posted @ 2015-01-27 18:50
李舜阳
阅读(157)
评论(0)
推荐(0)
摘要:
原题地址基本动态规划题代码: 1 int uniquePathsWithObstacles(vector > &obstacleGrid) { 2 if (obstacleGrid.empty() || obstacleGrid[0].empty()) return 0; 3 ... 阅读全文
posted @ 2015-01-27 18:31
李舜阳
阅读(144)
评论(0)
推荐(0)
摘要:
原题地址基本动态规划题代码: 1 int uniquePaths(int m, int n) { 2 vector sum(n, 0); 3 4 sum[n - 1] = 1; 5 for (int i = m - 1; i >= 0... 阅读全文
posted @ 2015-01-27 18:24
李舜阳
阅读(171)
评论(0)
推荐(0)
摘要:
原题地址我一直不太理解为什么叫rotate,翻译成"旋转"吧,似乎也不像啊。比如:1->2->3->4->5->NULL向右旋转2的距离,变成了:4->5->1->2->3->NULL后来琢磨半天+跟人讨论,似乎可以这么理解"rotate"1. 循环shift。这个比较容易理解。2. 环旋转。意思是... 阅读全文
posted @ 2015-01-27 18:06
李舜阳
阅读(258)
评论(0)
推荐(0)
摘要:
原题地址一个一个模拟肯定要超时,只有生算找规律呗。比如n=4,k=10,先将n=4的所有排列写出来:(1) 1 2 3 4(2) 1 2 4 3(3) 1 3 2 4(4) 1 3 4 2(5) 1 4 2 3(6) 1 4 3 2(7) 2 1 3 4(8) 2 1 4 3(9) ... 阅读全文
posted @ 2015-01-27 17:18
李舜阳
阅读(323)
评论(0)
推荐(0)
摘要:
原题地址相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些。注意当n是奇数的时候,中心小块要单独赋值(代码21行)代码: 1 vector > generateMatrix(int n) { 2 vector > matrix(n, vector(n, 0)... 阅读全文
posted @ 2015-01-27 15:32
李舜阳
阅读(157)
评论(0)
推荐(0)
摘要:
原题地址经典题目。所有数字异或,剩下的就是只出现一次的数字,因为其他出现两次的数字都没啦。T ^ T = 0代码:1 int singleNumber(int A[], int n) {2 int res = 0;3 4 for (int i = 0; i < n; i++)5 re... 阅读全文
posted @ 2015-01-27 15:19
李舜阳
阅读(149)
评论(0)
推荐(0)
摘要:
原题地址排序+DFS与Combination Sum II(参见这篇文章)不同的地方在于,重复的数字可以使用多次,无所谓啦,反正代码几乎都一样。代码: 1 vector > res; 2 3 void dfs(vector &candidates, vector ans, int pos, int... 阅读全文
posted @ 2015-01-27 15:16
李舜阳
阅读(148)
评论(0)
推荐(0)
摘要:
原题地址跟Jump Game II(见这篇文章)不同的地方在于,只需要判断能否到达终点即可遍历每个位置,更新能够到达的范围,最后看看范围是否覆盖了终点代码: 1 bool canJump(int A[], int n) { 2 int range = 0; 3 int... 阅读全文
posted @ 2015-01-27 14:54
李舜阳
阅读(159)
评论(0)
推荐(0)
摘要:
原题地址简单模拟,用t,b,l,r分别表示当前的上下左右四个边界,然后遍历即可代码: 1 vector spiralOrder(vector > &matrix) { 2 if (matrix.empty() || matrix[0].empty()) return vector(); 3 4... 阅读全文
posted @ 2015-01-27 14:43
李舜阳
阅读(266)
评论(0)
推荐(0)
摘要:
原题地址方法I:动态规划另sum[i]表示从i开始的最大子串和,则有递推公式:sum[i] = max{A[i], A[i] + sum[i+1]}因为递推式只用到了后一项,所以在编码实现的时候可以进行状态压缩,用一个变量即可代码: 1 int maxSubArray(int A[], int n)... 阅读全文
posted @ 2015-01-27 14:02
李舜阳
阅读(2789)
评论(0)
推荐(0)
摘要:
原题地址排序+DFS代码: 1 vector > res; 2 3 void dfs(vector &num, vector &ans, int pos, int left) { 4 if (left == 0) 5 res.push_back(ans); 6 for (i... 阅读全文
posted @ 2015-01-27 11:00
李舜阳
阅读(532)
评论(0)
推荐(0)
摘要:
原题地址想当年初学C++的时候写个大整数加法都快屎了,现在觉得自己还是成长了不少哇。代码: 1 string multiply(string num1, string num2) { 2 int len1 = num1.length(); 3 int len2 = n... 阅读全文
posted @ 2015-01-27 02:27
李舜阳
阅读(156)
评论(0)
推荐(0)
摘要:
原题地址普通分治题。需要注意n可正可负。代码: 1 double pow(double x, int n) { 2 if (n == 0) return 1; 3 if (n == 1) return x; 4 if (n == -1) return ... 阅读全文
posted @ 2015-01-27 02:12
李舜阳
阅读(143)
评论(0)
推荐(0)
摘要:
原题地址Anagram:变位词。两个单词是变位词关系的条件是:组成单词的字符相同,只是顺序不同第一次看这道题看了半天没明白要干嘛,丫就不能给个样例输入输出么。。后来还是看网上其他人的总结知道是怎么回事。通常的做法是:把字符串内的字符排序,这样,凡是变位词都会变成相同的单词。用map记录这样的单词出现... 阅读全文
posted @ 2015-01-27 02:00
李舜阳
阅读(196)
评论(0)
推荐(0)
摘要:
原题地址普遍的做法是:用栈保存从根节点到当前尚未遍历过的最小节点的路径(栈顶始终是最小节点)constructor:遍历至最小节点,同时将路径上出现的节点压栈保存hasNext:返回栈里面是否还有元素next:栈顶元素即为所求,弹栈的同时更新栈,使栈里始终保存的是从根节点到剩余未遍历节点的最小节点的... 阅读全文
posted @ 2015-01-27 01:49
李舜阳
阅读(549)
评论(0)
推荐(0)

浙公网安备 33010602011771号