上一页 1 2 3 4 5 6 7 8 9 10 ··· 16 下一页
摘要: bug为while(s1[j]==s2[k]&&j++<s1.size()&&k++<s2.size())这句,一开始写的是while(j++<s1.size()&&k++<s2.size()&&s1[j]==s2[k])class Solution {public: string longestCommonPrefix(vector<string> &strs) { if(strs.size()==0)return ""; int minlen=INT_MAX; f 阅读全文
posted @ 2013-05-24 19:49 代码改变未来 阅读(605) 评论(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:TreeNode *p,*q;TreeNode *prev; void recoverTree(TreeNode *root) { p=q=prev=NULL;... 阅读全文
posted @ 2013-05-24 14:28 代码改变未来 阅读(1476) 评论(0) 推荐(0) 编辑
摘要: 大整数乘法注意使用额外的空间会超时。class Solution {public: string multiply(string num1, string num2) { reverse(num1.begin(),num1.end()); reverse(num2.begin(),num2.end()); int l1=num1.size(); int l2=num2.size(); string v(l1+l2,'0'); int s; int multi; int c... 阅读全文
posted @ 2013-05-24 01:18 代码改变未来 阅读(359) 评论(0) 推荐(0) 编辑
摘要: jump gameclass Solution {public: bool canJump(int A[], int n) { int maxi; maxi=A[0]; for(int i=1;i<n-1;i++) { if(maxi<i)return false; if(i+A[i]>maxi) { maxi=i+A[i]; } } if(maxi>=n-1)return true; ... 阅读全文
posted @ 2013-05-23 16:34 代码改变未来 阅读(401) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int lengthOfLongestSubstring(string s) { int maxlen=0; if(s=="")return 0; vector<int>v(s.size(),1); int i; for(i=1;i<s.size();i++) { for(int j=1;j<=v[i-1];j++) { if(s[i]!=s[i-j]) v[i]++... 阅读全文
posted @ 2013-05-22 21:51 代码改变未来 阅读(148) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int atoi(const char *str) { int total; /* current total */ char sign; /* if '-', then negative, otherwise positive */ while ( isspace(*str) ) ++str; if (*str == '-' || *str == '+') sign = *str++; /* ... 阅读全文
posted @ 2013-05-22 17:01 代码改变未来 阅读(188) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: bool isValid(string s) { stack<char>sc; for(int i=0;i<s.size();i++) { if(s[i]=='('||s[i]=='['||s[i]=='{') sc.push(s[i]); if(s[i]==')') { if(!sc.empty()&&sc.top()=='(')sc.pop(); ... 阅读全文
posted @ 2013-05-22 14:52 代码改变未来 阅读(145) 评论(0) 推荐(0) 编辑
摘要: 判断是否回文class Solution {public: bool isPalindrome(string s) { if(s=="")return true; for(int i=0,j=s.size()-1;i<j;) { while(!isalpha(s[i])&&i<j&&!isdigit(s[i]))i++; while(!isalpha(s[j])&&i<j&&!isdigit(s[j]))j--; if(tolower(s[i])==tolower(s[j]... 阅读全文
posted @ 2013-05-22 14:29 代码改变未来 阅读(183) 评论(0) 推荐(0) 编辑
摘要: 细节很多,不是很容易做对class Solution {public: bool isNumber(const char *s) { bool flag; while(*s==' ') s++; const char *s2=s+strlen(s)-1; while(*s2==' ') { s2--; } if(*s=='+'||*s=='-')s++; const char *s1=s; int point=0; ... 阅读全文
posted @ 2013-05-22 14:06 代码改变未来 阅读(248) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int searchInsert(int A[], int n, int target) { int low=0,high=n-1; int m=0; if(A[0]>target)return 0; while(low<=high) { m=low+(high-low)/2; if(A[m]==target)return m; if(A[m]>target) { ... 阅读全文
posted @ 2013-05-22 00:46 代码改变未来 阅读(171) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 9 10 ··· 16 下一页