摘要: Q:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.For example:Given array A =[2,3,1,1,4]The minimum 阅读全文
posted @ 2013-09-29 22:33 summer_zhou 阅读(168) 评论(0) 推荐(0) 编辑
摘要: f(n) = f(n-1)+f(n-2) DP int climbStairs(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n<=2) return n; int a = 1,b = 2; int cnt; for(int i=3;i<=n;i++) { cnt = a+b; a = b; ... 阅读全文
posted @ 2013-09-29 16:56 summer_zhou 阅读(131) 评论(0) 推荐(0) 编辑
摘要: Q: 0-1背包问题。dp[i][j]: 表示S[0,i-1]包含T[0,j-1]的子序列个数。那么dp[i][j] = dp[i-1][j] + (S[i]==T[j]?dp[i-1][j-1]:0);因此时间复杂度为O(m+n)。空间复杂度可以优化到O(n). m表示S的长度,n表示T的长度。dp[j]表示第i次迭代的值。 int numDistinct(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function ... 阅读全文
posted @ 2013-09-29 16:48 summer_zhou 阅读(119) 评论(0) 推荐(0) 编辑
摘要: 递归 int maxDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return 0; int ld = maxDepth(root->left); int rd = maxDepth(root->right); return max(ld+1,rd+1); } 阅读全文
posted @ 2013-09-29 10:46 summer_zhou 阅读(125) 评论(0) 推荐(0) 编辑
摘要: 树的问题,优先考虑递归。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isSameTree(TreeNode *p, TreeNode *q) { // Start typing your C/C++ solu... 阅读全文
posted @ 2013-09-29 10:36 summer_zhou 阅读(152) 评论(0) 推荐(0) 编辑