05 2013 档案
摘要:class Solution {public: int maximalRectangle(vector<vector<char> > &matrix) { if (matrix.empty()) { return 0; } int n = matrix[0].size(); vector<int> H(n); vector<int> L(n); vector<int> R(n, n); int ret = 0; for (int i = 0; i < ma...
阅读全文
摘要:Iclass Solution {public: int maxProfit(vector<int> &prices) { if(prices.size()==0||prices.size()==1)return 0; int min=prices[0]; int max=prices[0]; int diff=0; for(int i=1;i<prices.size();i++) { if(prices[i]<min)min=prices[i]; ...
阅读全文
摘要:I动态规划,一次性构造,查询。采用回溯法会超时class Solution {public:int grid[100][100]; int uniquePaths(int m, int n) { if(grid[m-1][n-1]==0) construct(); return grid[m-1][n-1]; } void construct() { grid[0][0]=1; for(int i=1;i<100;i++) { grid[i][0]=1; } ...
阅读全文
摘要:回文数字,比较简单class Solution {public: bool isPalindrome(int x) { if(x<0)return false; int y=x; int z=0; while(y>0) { int k=y%10; z=z*10+k; y=y/10; } if(z==x)return true; else return false; }};
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *head; ListNode *p=l1,*q=l2; if(p=...
阅读全文
摘要:bug:忘记i>0的判断class Solution {public: void nextPermutation(vector<int> &num) { int i=num.size()-1; while(i>0&&num[i]<=num[i-1]) i--; if(i==0)sort(num.begin(),num.end()); else { int k=num[i-1]; int diff=INT_MAX; int inde...
阅读全文
摘要:用vector代替stackclass Solution {public:vector<string>v1; string simplifyPath(string path) { v1.clear(); string s; string temp; for(int i=0;i<path.size();) { if(path[i]=='/') { int j=i+1; temp=""; ...
阅读全文
摘要:不说了,贴代码class Solution {public: ListNode *reverseBetween(ListNode *head, int m, int n) { if(head==NULL)return NULL; ListNode *p=head; int i=1; for(;i<m;i++) p=p->next; for(int j=m;j<n;j++) { ListNode *q=p; for(int k=j;k<n;k++) ...
阅读全文
摘要:I不要用排列组合,会导致溢出class Solution {public: vector<vector<int> > generate(int numRows) { vector<int>v1; vector<vector<int> >v; for(int i=0;i<numRows;i++) { v1.clear(); for(int j=0;j<=i;j++) { if(j==0||j==i) ...
阅读全文
摘要:I:传统n后问题class Solution {public:vector<string>v1;vector<vector<string>>v;vector<int>x; vector<vector<string> > solveNQueens(int n) { x.clear(); for(int i=0;i<n;i++) x.push_back(0); v.clear(); dfs(0,n); return v; } void dfs(int depth,int n) ...
阅读全文
摘要:class Solution {public: int minPathSum(vector<vector<int> > &grid) { int m=grid.size(); if(m==0)return 0; int n=grid[0].size(); for(int i=1;i<m;i++) grid[i][0]+=grid[i-1][0]; for(int i=1;i<n;i++) grid[0][i]+=grid[0][i-1]; for(int ...
阅读全文
摘要:class Solution {public:vector<int>v1;vector<string>v;int count; string inttostring(int n) { stringstream ss; string str; ss<<n; ss>>str; return str; } vector<string> restoreIpAddresses(string s) { v.clear(); if(s.size()>12)return v...
阅读全文
摘要:Iclass Solution {public: ListNode *deleteDuplicates(ListNode *head) { if(head==NULL)return NULL; ListNode *p=head; while(p&&p->next) { ListNode *q=p->next; if(q->val==p->val) { if(q->next) { p->...
阅读全文
摘要:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:vector<int>v1;vector<vector<int>>v;int countsum; vector<vector<int> > pathSum(TreeNo
阅读全文
摘要: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
阅读全文
摘要:好方法!/** * 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;...
阅读全文
摘要:大整数乘法注意使用额外的空间会超时。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...
阅读全文
摘要: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; ...
阅读全文
摘要: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]++...
阅读全文
摘要: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++; /* ...
阅读全文
摘要: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(); ...
阅读全文
摘要:判断是否回文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]...
阅读全文
摘要:细节很多,不是很容易做对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; ...
阅读全文
摘要: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) { ...
阅读全文
摘要:算法1。动态规划dp[i][j] 表示的是 从i 到 j 的字串,是否是回文串。则根据回文的规则我们可以知道:如果s[i] == s[j] 那么是否是回文决定于 dp[i+1][ j - 1]当 s[i] != s[j] 的时候, dp[i][j] 直接就是 false。动态规划的进行是按照字符串的长度从1 到 n推进的。代码很明晰:给出java代码,复杂度 O(n^2)public class Solution { boolean[][] dp; public String longestPalindrome(String s) { if(s.length() == 0) ...
阅读全文
摘要:注意数组定义在leetcode里面必须指定长度。class Solution {public:char a[8]="MDCLXVI";int b[7]={1000,500,100,50,10,5,1}; int romanToInt(string s) { int k=0; int j=0; int i=0; for(;i<=6;i++) { if(i%2==0) { if(s[j]==a[i]) { ...
阅读全文
摘要:class Solution {public: vector<int> twoSum(vector<int> &numbers, int target) { vector<int>output; int i,j; for(i=0;i<numbers.size()-1;i++) { for(j=numbers.size()-1;j>i;j--) { if(numbers[i]+numbers[j]==target) { ...
阅读全文
摘要:class Solution {public:vector<int>v1;vector<vector<int>>v; vector<vector<int> > permuteUnique(vector<int> &num) { v.clear(); sort(num.begin(),num.end()); backtrace(0,num); return v; } void backtrace(int depth,vector<int> &num) { if(depth==nu...
阅读全文
摘要:合并排序后的数组从长数组的尾部开始合并,时间复杂度O(m+n),空间复杂度O(1)class Solution {public: void merge(int A[], int m, int B[], int n) { int i=m-1; int j=n-1; int k=m+n-1; while(i>=0&&j>=0) { if(A[i]>B[j]) A[k--]=A[i--]; else A[k--]=...
阅读全文
摘要:合并排序后的数组方法:从长数组的尾部开始,可以时间复杂度O(m+n),空间复杂度O(1)class Solution {public: void merge(int A[], int m, int B[], int n) { int i=m-1; int j=n-1; int k=m+n-1; while(i>=0&&j>=0) { if(A[i]>B[j]) A[k--]=A[i--]; else A[k-...
阅读全文
摘要:题目不难,边界情况很多class Solution {public: int lengthOfLastWord(const char *s) { const char *s1=s+strlen(s)-1;; int i=0; while(*s1==' ')//结尾有空格 s1--; while(*s1!=' '&&s1>=s)//不含空格的情况 { i++; s1--; } return i; ...
阅读全文
摘要:class Solution {public: vector<int> plusOne(vector<int> &digits) { reverse(digits.begin(),digits.end()); int s=digits[0]+1; int carry=0; if(s>=10)carry=1; else digits[0]=s; if(carry) { for(int i=0;i<digits.size();i++) { ...
阅读全文
摘要:Rotate Listclass Solution {public: ListNode *rotateRight(ListNode *head, int k) { if(head==NULL)return NULL; ListNode *p=head; int n=0; while(p->next) { p=p->next; n++; } n++; k=k%n; p->next=head; ListN...
阅读全文
摘要:Merge Intervals合并区间/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ bool comp(const Interval &a, const Interval &b){ if(a.start==b.start)return a.end<b.end; ...
阅读全文
摘要:字符串处理与二维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...
阅读全文
摘要:合并数组求中位数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...
阅读全文
摘要: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; ...
阅读全文
摘要:用减法会超时,所以用除数倍增的方法来实现除法。以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比较, 然
阅读全文
摘要: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); ...
阅读全文
摘要: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
阅读全文
摘要: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<...
阅读全文
摘要: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; ...
阅读全文
摘要: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) { ...
阅读全文
摘要: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[]...
阅读全文
摘要:斐波那契数列的变形,用递归会过不了large judge。所以采用另外一种方法。class Solution {public: int climbStairs(int n) { if(n==0)return 1; if(n==1)return 1; int a=1; int b=1; int c; for(int i=2;i<=n;i++) { c=a+b; a=b; b=c; } return c...
阅读全文
摘要:1.二叉树转链表Flatten Binary Tree to Linked ListGiven a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:1 \ 2 \ 3 \ 4 \ 5 \ ...
阅读全文
摘要:二叉树层序遍历,然后串成一个单链表,不允许用队列,要求空间复杂度为常数struct Node { int value; Node *left; Node *right; Node *next;};Node *Func(Node *root) { if (NULL == root) return NULL; Node *p1 = root, *p2 = root; p2->next = p2->left; if (NULL != p2->left) { p2->left->next = p2->right; els...
阅读全文
摘要:把每个string按字母排个序,然后就能很方便的进行string的两两比较class Solution { private: vector<string> ret; map<string, vector<string> > m; public: vector<string> anagrams(vector<string> &strs) { ret.clear(); m.clear(); for(int i = 0; i < strs.size(); i++) { string sor...
阅读全文
摘要:The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. For example, given n = 2, return [0,1,3,2]. Its gray
阅读全文
摘要:4-sum 已通过测试定义一对指针,指向两头。再定义一对指针,指向中间的两个元素。加起来看看跟target比较一下。再决定内部指针怎么移动。class Solution {public: vector<vector<int> > fourSum(vector<int> &num, int target) { vector<int>v; vector<vector<int>>v1; int len=num.size(); if(len<=3)return v1; sort(num.begin(),num.end
阅读全文