上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 16 下一页
摘要: 字符串处理与二维dp设置二维数组v[i][j]表示S串中前i个字母的子串中包含多少个T串中前j个字母的子串”这样的问题记为A[i][j]。 可以得到递推式 :if(S[i-1] == T[j-1]) A[i][j] = A[i-1][j-1] + A[i-1][j];else A[i][j] = A[i-1][j];class Solution {public: int numDistinct(string S, string T) { int m=S.size(); int n=T.size(); vector<vector<int>> v(m... 阅读全文
posted @ 2013-05-18 21:37 代码改变未来 阅读(425) 评论(0) 推荐(0) 编辑
摘要: 合并数组求中位数class Solution {public: double findMedianBaseCase(int med, int C[], int n) { if (n == 1) return (med+C[0])/2.0; if (n % 2 == 0) { int a = C[n/2 - 1], b = C[n/2]; if (med <= a) return a; else if (med <= b) return med; else /* med > b */ return b; } else { int a = C[n/2... 阅读全文
posted @ 2013-05-18 20:26 代码改变未来 阅读(481) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int maxSubArray(int A[], int n) { int sum=A[0]; int b=sum; for(int i=0;i<n;i++) { if(i>0)sum+=A[i]; if(sum>b)b=sum; if(sum<0&&i!=n-1) { sum=0; } } return b; ... 阅读全文
posted @ 2013-05-18 15:20 代码改变未来 阅读(106) 评论(0) 推荐(0) 编辑
摘要: 用减法会超时,所以用除数倍增的方法来实现除法。以100/3为例.算法分别比较97, 94, 91, ..., 4,1, -2,最后dividend = -2退出while循环.算法比较了34次.如果采用每次采用将比较数翻倍的比较方法. 算法会得到优化. 举例如下: k初始化为0, res = 0;首先用3与100比,小于. 然后翻倍6, 小于. 12, 24, 48, 96, 192, 因为192 > 100. 退回到 96. 这里共比较了 5次. 每比较一次 k++, res += 1<<k.100 - 96 = 4 > 除数3. 再用4重做上一步. 先跟3比较, 然 阅读全文
posted @ 2013-05-17 16:23 代码改变未来 阅读(284) 评论(0) 推荐(0) 编辑
摘要: 1.前序中序确定class Solution {public: TreeNode *buildTree(vector &preorder, vector &inorder) { if(preorder.size()==0||inorder.size()==0||preorder.size()!=inorder.size())return NULL; TreeNode *root=NULL; build(root,preorder,inorder,0,preorder.size()-1,0,inorder.size()-1); ... 阅读全文
posted @ 2013-05-15 23:34 代码改变未来 阅读(177) 评论(0) 推荐(0) 编辑
摘要: Combination Sum回溯法应用数组不含重复元素,结果可含重复元素。回溯法。自己写的代码:class Solution {public:vector<vector<int>>v;vector<int>v1;int sum; vector<vector<int> > combinationSum(vector<int> &candidates, int target) { sort(candidates.begin(),candidates.end()); v.clear(); sum=0; backtrac 阅读全文
posted @ 2013-05-15 00:47 代码改变未来 阅读(277) 评论(0) 推荐(0) 编辑
摘要: n个数取k个的集合自己写的代码:class Solution {public: vector<int>v1; vector<vector<int>>v; vector<vector<int>> combine(int n, int k) { v.clear(); vector<int>exist(n,0); add(exist,n,k,0); return v; } void add(vector<int>&exist,int n,int k,int cur) { if(cur<... 阅读全文
posted @ 2013-05-14 22:55 代码改变未来 阅读(200) 评论(0) 推荐(0) 编辑
摘要: 1.Longest Consecutive Sequence自动插入排序class Solution { public: int longestConsecutive(vector<int> &num) { map<int,int>hmap; hmap.clear(); int n = num.size(); for(int i=0; i<n; i++) { hmap[num[i]]=1; } int ans = 1; int max=1; ... 阅读全文
posted @ 2013-05-13 23:24 代码改变未来 阅读(230) 评论(0) 推荐(0) 编辑
摘要: Implement strStr()Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.class Solution {public: char *strStr(char *haystack, char *needle) { char *s,*t; int i=0,j=strlen(haystack)-strlen(needle); while(i++<=j) { ... 阅读全文
posted @ 2013-05-11 17:35 代码改变未来 阅读(146) 评论(0) 推荐(0) 编辑
摘要: First Missing PositiveGiven an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.关键是O(n)复杂度及常量运行空间。采用交换法。class Solution {public: int firstMissingPositive(int A[]... 阅读全文
posted @ 2013-05-11 15:08 代码改变未来 阅读(175) 评论(0) 推荐(0) 编辑
上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 16 下一页