diff --git a/15.3Sum1.cpp b/15.3Sum1.cpp new file mode 100644 index 0000000..7b62028 --- /dev/null +++ b/15.3Sum1.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include + +using namespace std; + +vector > threeSum(vector& nums) + { + vector > result; + if (nums.size()<3) return result; + int target=0; + sort(nums.begin(),nums.end()); + for (int head=0;head one; + one.push_back(nums[head]); + target=target-nums[head]; + map nummap; + int flag=nums[nums.size()-1]+1; + for (int i=head+1;i(nums[i],i)); + } + } + } + return result; + } + +int main() +{ + ifstream fin("in.txt"); + vector nums; + int num_tmp; + while(!fin.eof()) + { + fin>>num_tmp; + nums.push_back(num_tmp); + } +// for(int i=0;i > result=threeSum(nums); + for(int i=0;i +#include +using namespace std; + +string addBinary(string a, string b) { + int lena=a.size(); + int lenb=b.size(); + + int re[max(lena,lenb)+1]; + re[0]=0; + + int i=0; + for (i=0;i0;j--) + { + if (re[j]>1) + { + re[j-1]+=re[j]/2; + re[j]=re[j]%2; + } + } + + string res; + for (int k=max(lena,lenb);k>=0;k--) + { + res.push_back('0'+re[max(lena,lenb)-k]); + } + if (re[0]==0) + { + res=res.substr(1); + } + return res; + } + + +int main() +{ + string a="0",b="1"; + string c=addBinary(a,b); + cout< +using namespace std; + +int addDigits(int num) { + while (num/10!=0) + { + int sum=0; + int tmp=num; + while (tmp!=0) + { + sum+=tmp%10; + tmp/=10; + } + num=sum; + } + return num; + } + + int main() + { + int a=347; + int c=addDigits(a); + cout< +#include +using namespace std; + +struct TreeNode +{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x):val(x),left(NULL),right(NULL){} +}; +int depth(TreeNode* root,bool& isB){ + if (root->left==NULL&&root->right==NULL) + { + return 1; + } + int llen,rlen; + if (root->left!=NULL) + llen=depth(root->left,isB); + else + llen=0; + if (root->right!=NULL) + rlen=depth(root->right,isB); + else + rlen=0; + if (abs(llen-rlen)>1) + { + isB=false; + return 0; + } + return llen>rlen?(llen+1):(rlen+1); + } + bool isBalanced(TreeNode* root) { + if (root==NULL) + return true; + bool isB=true; + depth(root,isB); + return isB; + } + +int main() +{ + TreeNode* root=new TreeNode(1); + root->left=new TreeNode(2); + root->right=new TreeNode(3); + root->left->left=new TreeNode(5); + bool isB; + isB=isBalanced(root); + cout< +#include +using namespace std; + +int maxProfit(vector& prices) +{ + int len = prices.size(); + if (len == 0) + return 0; + int low = prices[0]; + int high; + int sum=0; + for (int i = 1; i < len; i++) + if (prices[i]>prices[i-1]) + if (i == len-1) + sum += prices[i] - low; + else + continue; + else + { + high = prices[i-1]; + sum = high - low + sum; + low = prices[i]; + } + return sum; +} + +int main() +{ + vector nums; + nums.push_back(1); + nums.push_back(2); + cout<& prices) +{ + int len = prices.size(); + if (len < 2) + return 0; + int buys[len]; + int sells[len]; + sells[0]=0;sells[1]=max(0, prices[1] - prices[0]); + buys[0]= -prices[0];buys[1]=max(-prices[0], -prices[1]); + + for (int i = 2 ; i < len ; i++) + { + sells[i] = max(sells[i-1], buys[i-1] + prices[i]); + buys[i] = max(buys[i-1], sells[i-2] - prices[i]); + } + return sells[len-1]; +} diff --git a/Best Time to Buy and Sell Stock.cpp b/Best Time to Buy and Sell Stock.cpp new file mode 100644 index 0000000..c4bc0dd --- /dev/null +++ b/Best Time to Buy and Sell Stock.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +int maxProfit(vector& prices) +{ + int len = prices.size(); + if (len == 0) + return 0; + int out[len]; + out[0] = 0; + int minele = prices[0]; + + for (int i = 1; i < len ; i++) + { + if (prices[i]-minele > out[i-1]) + { + out[i] = prices[i]-minele; + minele = min(prices[i],minele); + } + else + { + out[i] = out[i-1]; + minele = min(prices[i],minele); + } + } + return out[len-1]; +} + +int main() +{ + vector nums; + nums.push_back(2); + nums.push_back(1); + nums.push_back(4); + int re = maxProfit(nums); + cout< stk; + BSTIterator(TreeNode* root) + { + while (root) + { + stk.push(root); + root = root->left; + } + } + + bool hasNext() + { + return !stk.empty(); + } + + int next() + { + TreeNode* tmp = stk.top(); + int res = tmp->val; + stk.pop(); + tmp = tmp->right; + while(tmp) + { + stk.push(tmp); + tmp = tmp->left; + } + return res; + } +}; diff --git a/Binary Tree Inorder Traversal.cpp b/Binary Tree Inorder Traversal.cpp new file mode 100644 index 0000000..5e72679 --- /dev/null +++ b/Binary Tree Inorder Traversal.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +struct TreeNode{ + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x):val(x),left(NULL),right(NULL){} +}; + +void cur(vector& re,TreeNode* t) + { + if (t->left!=NULL) + cur(re,t->left); + re.push_back(t->val); + if (t->right!=NULL) + { + cur(re,t->right); + } + } + vector inorderTraversal(TreeNode* root) { + vector re; + if (root==NULL) + return re; + cur(re,root); + return re; + } + +int main() +{ + TreeNode* root=new TreeNode(1); + root->right=new TreeNode(2); + root->right->left=new TreeNode(3); + vector re; + re=inorderTraversal(root); + for (int i=0;i +#include +#include +using namespace std; + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x):val(x),left(NULL),right(NULL){} +}; + +vector > levelOrderBottom(TreeNode* root) +{ + vector > re; + vector rec; + queue q1; + TreeNode* t=root; + if (!t) + return re; + q1.push(t); + while (!q1.empty()) + { + queue q2; + while (!q1.empty()) + { + t=q1.front(); + rec.push_back(t->val); + q1.pop(); + if (t->left) + q2.push(t->left); + if (t->right) + q2.push(t->right); + } + re.insert(re.begin(),rec); + rec.clear(); + q1=q2; + } + return re; +} + +int main() +{ + TreeNode* root=new TreeNode(1); + vector > re; + re=levelOrderBottom(root); + return 0; +} diff --git a/Binary Tree Level Order Traversal.cpp b/Binary Tree Level Order Traversal.cpp new file mode 100644 index 0000000..494ab87 --- /dev/null +++ b/Binary Tree Level Order Traversal.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +using namespace std; + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x):val(x),left(NULL),right(NULL){} +}; + +vector > levelOrder(TreeNode* root) +{ + vector > re; + vector rec; + queue q1; + TreeNode* t=root; + if (!t) + return re; + q1.push(t); + while (!q1.empty()) + { + queue q2; + while (!q1.empty()) + { + t=q1.front(); + rec.push_back(t->val); + q1.pop(); + if (t->left) + q2.push(t->left); + if (t->right) + q2.push(t->right); + } + re.push_back(rec); + rec.clear(); + q1=q2; + } + return re; +} + +int main() +{ + TreeNode* root=new TreeNode(1); + vector > re; + re=levelOrder(root); + return 0; +} diff --git a/Binary Tree Maximum Path Sum.cpp b/Binary Tree Maximum Path Sum.cpp new file mode 100644 index 0000000..3b9dd61 --- /dev/null +++ b/Binary Tree Maximum Path Sum.cpp @@ -0,0 +1,33 @@ +#include"tree.h" + +int ans; + +int scanT(TreeNode* t) +{ + if (t==NULL) + return 0; + int left = scanT(t->left); + int right = scanT(t->right); + int val = root->val; + if (left>0) + val+=left; + if (right>0) + val+=right; + if (val>ans) + ans=val; + return max(root->val,max(left+root->val,right+root->val)); +} + +int maxPathSum(TreeNode *root) +{ + if (root==NULL) + return 0; + ans = root->val; + scanT(root); + return ans; +} + +int main() +{ + +} diff --git a/Binary Tree Paths.cpp b/Binary Tree Paths.cpp new file mode 100644 index 0000000..b171f88 --- /dev/null +++ b/Binary Tree Paths.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +using namespace std; + +struct TreeNode +{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x):val(x),left(NULL),right(NULL){} +}; + +void cur(vector& re,string& cur_re,TreeNode* t) +{ + stringstream ss; + ss<val; + string s; + ss>>s; + int len=s.size(); + if (t->left==NULL&&t->right==NULL) + { + cur_re=cur_re+s; + re.push_back(cur_re); + cur_re.erase(cur_re.end()-len,cur_re.end()); + return; + } + cur_re=cur_re+s+"->"; + if (t->left!=NULL) + { + cur(re,cur_re,t->left); + } + if (t->right!=NULL) + { + cur(re,cur_re,t->right); + } + cur_re.erase(cur_re.end()-len-2,cur_re.end()); +} + +vector binaryTreePaths(TreeNode* root) +{ + vector re; + string cur_re; + if (root==NULL) + return re; + cur(re,cur_re,root); + return re; +} +int main() +{ + TreeNode* root=new TreeNode(1); + root->left=new TreeNode(2); +// root->right=new TreeNode(3); + root->left->left=new TreeNode(5); + vector re=binaryTreePaths(root); + for (int i=0;i +#include + +vector rightSideView(TreeNode* root) +{ + vector re; + queue q; + if (root==NULL) + return re; + q.push(root); + while (!q.empty()) + { + int len = q.size(); + TreeNode* t; + for (int i=0;ileft!=NULL) + q.push(t->left); + if (t->right!=NULL) + q.push(t->right); + } + re.push_back(t->val); + } + return re; +} + +int main() +{ + ifstream f("in.txt"); + string s; + vector ss; + while (!f.eof()) + { + f>>s; + ss.push_back(s); + } + TreeNode* t = buildtree(ss); + vector re = rightSideView(t); + for (int i=0;i > zigzagLevelOrder(TreeNode* root) +{ + vector > re; + vector cur; + vector q1; + int k = 1; + if (root==NULL) + return re; + q1.push_back(root); + while (!q1.empty()) + { + int len = q1.size(); + vector q2; + if(k%2==0) + { + for (int i=len-1;i>=0;i--) + { + cur.push_back(q1[i]->val); + if (q1[len-1-i]->left) + q2.push_back(q1[len-1-i]->left); + if (q1[len-1-i]->right) + q2.push_back(q1[len-1-i]->right); + } + } + else + { + for (int i=0;ival); + if (q1[i]->left) + q2.push_back(q1[i]->left); + if (q1[i]->right) + q2.push_back(q1[i]->right); + } + } + q1 = q2; + re.push_back(cur); + k++;cout< ss; + while (!f.eof()) + { + f>>s; + ss.push_back(s); + } + TreeNode* t = buildtree(ss); + vector > re; + re = zigzagLevelOrder(t); + for (int i=0;i +#include +#include +#include +using namespace std; + +void sumc(vector >& re,vector candidates,vector& cur_re,int target,int sum,int index) + { + if (sum>target) + return; + if (sum==target){ + re.push_back(cur_re); + return; + } + for (int i=index;i > combinationSum(vector& candidates, int target) { + vector > re; + vector cur_re; + sort(candidates.begin(),candidates.end()); + sumc(re,candidates,cur_re,target,0,0); + return re; + } +void comb(vector candidates, int index, int sum, int target, vector > &res, vector &path) + { + if(sum>target)return; + if(sum==target){res.push_back(path);return;} + for(int i= index; i > combinationSum(vector &candidates, int target) { + // Note: The Solution object is instantiated only once. +// sort(candidates.begin(),candidates.end()); +// vector > res; +// vector path; +// comb(candidates,0,0,target,res,path); +// return res; +// } +int main() +{ + vector > re; + vector candidates; + ifstream fin("incombination sum.txt"); + while(!fin.eof()) + { + int a; + fin>>a; + candidates.push_back(a); + } + re=combinationSum(candidates,310); + for (int i=0;i +#include +#include +#include +using namespace std; + +void sumc(vector >& re,vector candidates,vector& cur_re,int target,int sum,int index) + { + if (sum>target) + return; + if (sum==target){ + re.push_back(cur_re); + return; + } + if (index>=candidates.size()) + return; + for (int i=index;i > combinationSum2(vector& candidates, int target) { + vector > re; + vector cur_re; + sort(candidates.begin(),candidates.end()); + sumc(re,candidates,cur_re,target,0,0); + return re; + } + +int main() +{ + vector > re; + vector candidates; + ifstream fin("incombination sum.txt"); + while(!fin.eof()) + { + int a; + fin>>a; + candidates.push_back(a); + } + re=combinationSum2(candidates,2); + for (int i=0;i +#include +#include +#include +using namespace std; + +void cur(vector >& re,vector cur_re,int k,int dep,int index,int sum,int target) +{ + if (dep==k) + { + if (sum==target) + { + re.push_back(cur_re); + return; + } + else + return; + } + if (sum>target) + return; + for (int i=index;i<10;i++) + { + cur_re.push_back(i); + cur(re,cur_re,k,dep+1,i+1,sum+i,target); + cur_re.pop_back(); + } +} + +vector > combinationSum3(int k,int n) +{ + vector > re; + vector cur_re; + cur(re,cur_re,k,0,1,0,n); + return re; +} + +int main() +{ + vector > re; + vector candidates; + re=combinationSum3(3,9); + for (int i=0;i +#include +using namespace std; + +void dfs(vector >& re,int dep,int num,vector& cur_re,int k,int n) + { + if (dep==k) + { + re.push_back(cur_re); + return; + } + for (int i=num;i<=n-k+dep+1;i++) + { + cur_re.push_back(i); + dfs(re,dep+1,i+1,cur_re,k,n); + cur_re.pop_back(); + } + } + vector > combine(int n, int k) { + vector > re; + vector cur_re; + + dfs(re,0,1,cur_re,k,n); + return re; + } + +int main() +{ + vector > re; + re=combine(4,2); + for (int i=0;i +#include +#include +using namespace std; + +bool containsNearbyDuplicate(vector& nums, int k) { + if (nums.size()<2) + return 0; + map nummap; + for (int i=0;i::iterator lit=nummap.find(nums[i]); + int j=lit->second; + if (i-j<=k) + return true; + else + nummap.erase(lit); + } + nummap.insert(pair(nums[i],i)); + } + return false; + } + +int main() +{ + map nummap; + nummap.insert(pair(5,1)); + nummap.insert(pair(4,2)); + nummap.insert(pair(5,3)); + nummap.insert(pair(5,4)); + nummap.insert(pair(4,5)); + + map::iterator lit=nummap.find(5); + cout<& nums,int high,int low) +{ + if (high < low) + return NULL; + int mid = (high+low)/2; + TreeNode* re = new TreeNode(nums[mid]); + + re->left = btree(nums,mid-1,low); + re->right = btree(nums,high,mid+1); + return re; +} + +TreeNode* sortedArrayToBST(vector& nums) +{ + int len = nums.size(); + int high = len - 1, low = 0; + + if (len == 0) + return NULL; + if (len == 1) + return new TreeNode(nums[0]); + + TreeNode* root = btree(nums,high,low); + return root; +} + +int main() +{ + vector nums; + for (int i=0;i<8;i++) + nums.push_back(i); + TreeNode* root = sortedArrayToBST(nums); + printtree(root); + return 0; +} diff --git a/Count Complete Tree Nodes.cpp b/Count Complete Tree Nodes.cpp new file mode 100644 index 0000000..2611027 --- /dev/null +++ b/Count Complete Tree Nodes.cpp @@ -0,0 +1,53 @@ +#include "tree.h" +#include + +int getleft(TreeNode* l) +{ + int cont = 0; + while (l!=NULL) + { + l=l->left; + cont++; + } + return cont; +} + +int getright(TreeNode* r) +{ + int cont = 0; + while (r!=NULL) + { + r=r->right; + cont++; + } + return cont; +} + +int countNodes(TreeNode* root) +{ + if (root==NULL) + return 0; + int contleft = getleft(root); + int contright = getright(root); + if (contleft == contright) + return pow(2,contleft)-1; + else + return countNodes(root->left) + countNodes(root->right) + 1; +} + +int main() +{ + ifstream f("in.txt"); + string s; + vector ss; + while (!f.eof()) + { + f>>s; + ss.push_back(s); + } + TreeNode* t = buildtree(ss); + int cont = countNodes(t); + cout< +#include +#include +using namespace std; + +//int countPrimes(int n) { +// vector num(n - 1, true); +// num[0] = false; +// int res = 0, limit = sqrt(n); +// for (int i = 2; i <= limit; ++i) { +// if (num[i - 1]) { +// for (int j = i * i; j < n; j += i) { +// num[j - 1] = false; +// } +// } +// } +// for (int j = 0; j < n - 1; ++j) { +// if (num[j]) ++res; +// } +// return res; +// } + +int countPrimes(int n) { + int cont=0; + bool isP[n-1]; + for (int i=0;i +#include +using namespace std; + +string countAndSay(int n) +{ + if (n==0) + return ""; + string re="1"; + string cont; + for (int i=0;i>tmp; + return tmp; +} + +int numDecodings(string s) +{ + int len = s.size(); + if (len==0) + return 0; + int cont[len]; + if (stringtoint(s.substr(0,1))>=1) + cont[0]=1; + else + cont[0]=0; + if (len==1) + return cont[0]; + if (cont[0]==0&&len!=1) + return 0; + int tmp = stringtoint(s.substr(0,2)); + if(tmp>=1&&tmp<=26) + if (stringtoint(s.substr(1,1))!=0) + cont[1]=2; + else + cont[1]=1; + else + if (stringtoint(s.substr(1,1))!=0) + cont[1]=1; + else + return 0; + for (int i=2;i=1&&tmp<=26) + if (stringtoint(s.substr(i,1))!=0&&stringtoint(s.substr(i-1,1))!=0) + cont[i]=cont[i-1]+cont[i-2]; + else + cont[i]=cont[i-2]; + else + if (stringtoint(s.substr(i,1))==0) + return 0; + else + cont[i]=cont[i-1]; + } + return cont[len-1]; +} + +int main() +{ + string s = "301"; + int re = numDecodings(s); + cout< +#include +using namespace std; + +int titleToNumber(string s) { + int len=s.size(); + int re=0; + for (int i=0;i +#include +using namespace std; + +string convertToTitle(int n) +{ + int level=1; + string re; + while(n!=0) + { + if (n%26==0) + { + re.push_back('Z'); + n=n/26-1; + } + else + { + re.push_back('A'+n%26-1); + n=n/26; + } + } + int high=re.size()-1,low=0; + while (low<=high) + { + char tmp=re[low]; + re[low]=re[high]; + re[high]=tmp; + low++; + high--; + } + return re; +} +int main() +{ + string a; + a=convertToTitle(1); + cout<left == NULL && root->right == NULL) + return; + if (root->left!=NULL) + flatten(root->left); + TreeNode* tmp = root->right; + root->right = root->left; + root->left = NULL; + while (root->right!=NULL) + root = root->right; + root->right = tmp; + flatten(tmp); +} + +int main() +{ + ifstream f("in.txt"); + string s; + vector ss; + while (!f.eof()) + { + f>>s; + ss.push_back(s); + } + TreeNode* t = buildtree(ss); + flatten(t); + printtree(t); + return 0; +} diff --git a/Generate Parentheses.cpp b/Generate Parentheses.cpp new file mode 100644 index 0000000..09317c5 --- /dev/null +++ b/Generate Parentheses.cpp @@ -0,0 +1,40 @@ +#include +#include +using namespace std; + +void cur(vector& re,string& cur_re,int leftn,int rightn) + { + if (leftn==0&&rightn==0) + { + re.push_back(cur_re); + return; + } + else + { + if (leftn>rightn) + return; + if (leftn!=0) + { + cur_re.push_back('('); + cur(re,cur_re,leftn-1,rightn); + cur_re.erase(cur_re.end()-1); + } + cur_re.push_back(')'); + cur(re,cur_re,leftn,rightn-1); + cur_re.erase(cur_re.end()-1); + } + } + vector generateParenthesis(int n) { + vector re; + string cur_re; + cur(re,cur_re,n,n); + return re; + } +int main() +{ + vector re; + re=generateParenthesis(3); + for (int i=0;i +#include +#include +using namespace std; + +int caqsum(int num){ + int sum=0; + while (num!=0){ + sum+=pow(num%10,2); + num/=10; + } + return sum; + } + bool isHappy(int n) { + int sum=caqsum(n); + map nummap; + int k=1; + while (sum!=1) + { + sum=caqsum(sum); + if (nummap.count(sum)) + {cout<(sum,k)); + k++; + } + return true; + } + + int main() + { + int a=7; + bool b=isHappy(a); + cout< +#include +using namespace std; + +int strStr(string haystack, string needle) { + int len=haystack.size(); + int lenn=needle.size(); + int pointh=0,pointn=0; + + if(lenn==0) + return 0; + for (int i=0;i +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +ListNode* insertionSortList(ListNode* head) { + ListNode* h=new ListNode(0); + h->next=head; + ListNode* tmp1=h; + ListNode* tmp2=head; + int k=0; + + if (head->next==NULL || head==NULL) + return head; + + while (tmp2->next!=NULL) + { + int flag=0; + k++; + for (int i=0;inext->val>tmp2->next->val) + { + if (i==0) + { + ListNode* tttt=tmp2->next->next; + tmp2->next->next=head; + head=tmp2->next; + tmp2->next=tttt; + flag=1; + break; + } + else + { + ListNode* tt=tmp2->next->next; + tmp2->next->next=tmp1->next; + tmp1->next=tmp2->next; + tmp2->next=tt; + flag=1; + break; + } + } + tmp1=tmp1->next; + } + if (flag==0) + { + tmp2=tmp2->next; + } + h->next=head; + tmp1=h; + } + return head; + } + +int main() +{ + ListNode* head=new ListNode(2); + head->next=new ListNode(4); + head->next->next=new ListNode(3); + head->next->next->next=new ListNode(5); + head->next->next->next->next=new ListNode(1); + head->next->next->next->next->next=new ListNode(0); + ListNode* p=insertionSortList(head); + while (p!=NULL) + { + cout<val<next; + } +return 0; +} diff --git a/Intersection of Two Linked Lists.cpp b/Intersection of Two Linked Lists.cpp new file mode 100644 index 0000000..750fa50 --- /dev/null +++ b/Intersection of Two Linked Lists.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +struct ListNode{ + int val; + ListNode* next; + ListNode(int x):val(x),next(NULL){} +}; + +ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + int cont1=0,cont2=0; + ListNode* tmp1=headA; + ListNode* tmp2=headB; + if (headA==NULL||headB==NULL) + return NULL; + while (tmp1!=NULL) + { + cont1++; + tmp1=tmp1->next; + } + while (tmp2!=NULL) + { + cont2++; + tmp2=tmp2->next; + } + if (cont1next; + } + for (int i=0;inext; + tmp2=tmp2->next; + } + return NULL; + } + +int main() +{ + ListNode* headA=new ListNode(1); + headA->next=new ListNode(2); + headA->next->next=new ListNode(3); + ListNode* headB=new ListNode(4); + headB->next=new ListNode(5); + headB->next->next=new ListNode(6); + headB->next->next->next=headA->next->next; + headA->next->next->next=new ListNode(7);cout<<9; + headA->next->next->next->next=new ListNode(8); + + ListNode* tmp=getIntersectionNode(headA,headB); + cout<val; + return 0; +} diff --git a/Isomorphic String.cpp b/Isomorphic String.cpp new file mode 100644 index 0000000..f5d662f --- /dev/null +++ b/Isomorphic String.cpp @@ -0,0 +1,15 @@ +#include +#include +#include +using namespace std; + +bool isIsomorphic(string s,string t) +{ + bool re; + map<> +} + +int main() +{ + +} diff --git a/Kth Smallest Element in a BST.cpp b/Kth Smallest Element in a BST.cpp new file mode 100644 index 0000000..8b7e309 --- /dev/null +++ b/Kth Smallest Element in a BST.cpp @@ -0,0 +1,45 @@ +#include "tree.h" + +int kthSmallest(TreeNode* root, int k) +{ + stack stk; + + while (root) + { + stk.push(root); + root = root->left; + } + + for (int i=0;ival; + stk.pop(); + if (t->right) + { + TreeNode* tmp = t->right; + while (tmp) + { + stk.push(tmp); + tmp = tmp->left; + } + } + } +} + +int main() +{ + ifstream f("in.txt"); + string s; + vector ss; + while (!f.eof()) + { + f>>s; + ss.push_back(s); + } + TreeNode* t = buildtree(ss); + int kthsmallest = kthSmallest(t,5); + cout< +#include +using namespace std; + +int lengthOfLastWord(string s) { + int len=s.size(); + if (len==0) + return 0; + int cont=0; + for (int i=len-1;i>=0;i--) + { + if (s[i]!=' ') + cont++; + else + break; + + } + return cont; + } + +int main() +{ + string s="a"; + int len=lengthOfLastWord(s); + cout< +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +ListNode *detectCycle(ListNode *head) { + ListNode* fast=head; + ListNode* slow=head; + + if (head==NULL) + return NULL; + + int step=0; + while (fast->next!=NULL) + { + fast=fast->next->next; + slow=slow->next; + if (fast==slow) + break; + } + if (fast->next==NULL) + return NULL; + while (true) + { + slow=slow->next; + head=head->next; + if (slow==head) + return slow; + } + } + +int main() +{ + ListNode* head=new ListNode(1); + head->next=new ListNode(2); + detectCycle(head); + return 0; +} diff --git a/Longest Common Prefix.cpp b/Longest Common Prefix.cpp new file mode 100644 index 0000000..36a4967 --- /dev/null +++ b/Longest Common Prefix.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +using namespace std; + +string longestCommonPrefix(vector& strs) +{ + string pre; + pre=strs[0]; + + for (int i=1;i maplist; + if (len==0 || len==1) + return len; + int left = 0, right = 1, maxlen=1, lentmp = 1; + maplist.insert(pair (s[left],1)); + while (right <= len-1){ + if (maplist.size() > 2){ + maplist[s[left]]--; + lentmp--; + cout< (s[right],1)); + right++; + lentmp++; + if (maplist.size()<=2) + maxlen = max(maxlen,lentmp); + } + } + return maxlen; + } + +int main() +{ + TreeNode *t; + if (t && t->left) + return 3; + return 0; +} diff --git a/Lowest Common Ancestor of a Binary Tree.cpp b/Lowest Common Ancestor of a Binary Tree.cpp new file mode 100644 index 0000000..87f3286 --- /dev/null +++ b/Lowest Common Ancestor of a Binary Tree.cpp @@ -0,0 +1,27 @@ +#include"tree.h" + +TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) +{ + if (root==NULL) + return NULL; + if (root==p||root==q) + return root; + TreeNode* l = lowestCommonAncestor(root->left,p,q); + TreeNode* r = lowestCommonAncestor(root->right,p,q); + if (l && r) + return root; + return l?l:R +} + +int main() +{ + ifstream f("in.txt"); + string s; + vector ss; + while (!f.eof()) + { + f>>s; + ss.push_back(s); + } + TreeNode* t = buildtree(ss); +} diff --git a/Maximal Square.cpp b/Maximal Square.cpp new file mode 100644 index 0000000..4fefe5d --- /dev/null +++ b/Maximal Square.cpp @@ -0,0 +1,57 @@ +#include "pack.h" + +int maximalSquare(vector >& matrix) +{ + int len = matrix.size(); + if (len == 0) + return 0; + int dp[matrix.size()][matrix[0].size()]; + for (int i=0;i 0 && j > 0) + if (matrix[i][j] =='1') + dp[i][j] = min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]) + 1; + else if (matrix[i][j] == '1') + dp[i][j] = 1; + if (dp[i][j] > maxarea) + maxarea = dp[i][j]; + } + } + return maxarea * maxarea; +} + +int main() +{ + ifstream in("in.txt"); + vector > sss; + vector t; + int k=0; + while (!in.eof()) + { + char tmp; + in>>tmp; + t.push_back(tmp); + k++; + if (k%5==0) + { + sss.push_back(t); + t.clear(); + } + } + for (int i=0;i +using namespace std; + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x):val(x),left(NULL),right(NULL){}; +}; + +void cur(TreeNode* t,int& cont,int dep) + { + if (t->left==NULL&&t->right==NULL) + { + dep++; + if (dep>cont) + cont=dep; + return; + } + dep++; + if (t->left!=NULL) + cur(t->left,cont,dep); + if (t->right!=NULL) + cur(t->right,cont,dep); + } + int maxDepth(TreeNode* root) { + if (root==NULL) + return 0; + int cont=1; + cur(root,cont,0); + return cont; + } + +int main() +{ + TreeNode* root=new TreeNode(1); + root->left=new TreeNode(2); + int a=maxDepth(root); + cout< +#include +using namespace std; + +int maxSubArray(vector& nums) +{ + int len=nums.size(); + vector cont(len,0); + cont[0]=nums[0]; + for (int i=1;i=0;j--) + { + cout<cont[i]) + cont[i]=sum; + sum+=nums[j]; + } + if (sum>cont[i]) + cont[i]=sum; + cout< nums; + nums.push_back(-2); + nums.push_back(1); + nums.push_back(-3); + nums.push_back(4); + nums.push_back(-1); + nums.push_back(2); + nums.push_back(1); + nums.push_back(-5); + nums.push_back(4); + int re=maxSubArray(nums); + cout< +#include +using namespace std; + +int main() +{ + vector a; + a.push_back(6); + a.push_back(5); + a.erase(a.begin()); + cout< +#include +using namespace std; + +int minPathSum(vector >& grid) { + if (grid.size()==0) + return 0; + int sp[grid.size()][grid[0].size()]; + sp[0][0]=grid[0][0]; + for (int i=1;isp[i][j-1]) + sp[i][j]=sp[i-1][j]+grid[i][j]; + else + sp[i][j]=sp[i][j-1]+grid[i][j]; + } + return sp[grid.size()-1][grid[0].size()-1]; + } + +int main() +{ + vector c({1,2}); + vector b({1,1}); + vector > re; + re.push_back(c); + re.push_back(b); + int r=minPathSum(re); + cout< +#include +#include +using namespace std; + +int minSubArrayLen(int s, vector& nums) +{ + int len=nums.size(); + int p1=0,p2=1; + int re; + if(len==0) + return 0; + if (nums[1]>=s) + return 1; + + int sum=nums[p1]+nums[p2]; + if (sum>=s) + re=2; + else + re=len; + int cont=2; + while (p2<=len-1) + { + if (sum=s) + { + sum-=nums[p1]; + p1++; + cont--; + if (cont<=re) + re=cont; + } + else + { + p1++; + p2++; + if(p2<=len-1) + sum=sum-nums[p1-1]+nums[p2]; + } + } + } + return re; +} + +int main() +{ + vector n; +// n.push_back(2); +// n.push_back(3); +// n.push_back(1); + n.push_back(1); + n.push_back(1); +// n.push_back(3); + int a=minSubArrayLen(3,n); + cout< +#include +using namespace std; + diff --git a/Number of Islands.cpp b/Number of Islands.cpp new file mode 100644 index 0000000..4a303dc --- /dev/null +++ b/Number of Islands.cpp @@ -0,0 +1,49 @@ +#include "pack.h" + +int numIslands(vector >& grid) +{ + int lenr = grid.size(); + if (lenr == 0) + return 0; + int lenc = grid[0].size(); + + int ans=0; + for (int i=0;i >& grid,int i,int j) +{ + int lenr = grid.size(); + int lenc = grid[0].size(); + + if (i+1==lenr&&j+1==lenc) + return; + else if (i+1==lenr) + { + if (grid[i][j+1]=='0') + return; + } + else if (j+1==lenc) + { + if (grid[i+1][j]=='0') + return; + } + else + { + if (grid[i+1][j]=='0'&&grid[i][j+1]=='0') + return; + } + if (grid[i][j]=='1') + grid[i][j]=0; + if (i+1 +using namespace std; + diff --git a/Palindrome Number.cpp b/Palindrome Number.cpp new file mode 100644 index 0000000..48af526 --- /dev/null +++ b/Palindrome Number.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +bool isPalindrome(int x) { + if (x<0) + return false; + int num=1; + while (x/num>=10) + { + num*=10; + } + int numl=1; + while (num>=numl) + { + int h=x/num; + int l=x%10; + if (h==l) + { + x-=x/num*num; + x/=10; + num/=100; + } + else + { + return false; + } + } + + return true; + } + +int main() +{ + int a=10; + bool c=isPalindrome(a); + cout< +#include +using namespace std; + +void cur(vector >& re,) +{ + +} + +vector > partition(string s) +{ + +} diff --git a/Pascal's Triangle II.cpp b/Pascal's Triangle II.cpp new file mode 100644 index 0000000..1731a85 --- /dev/null +++ b/Pascal's Triangle II.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; + +vector getRow(int rowIndex) { + vector re(rowIndex+1,0); + vector t(rowIndex+1,0); + t[0]=1; + for (int i=1;i re=getRow(0); + for (int i=0;i +#include +using namespace std; + +vector > generate(int numRows) { + vector > re; + vector v(1,1); + re.push_back(v); + for (int i=1;i u(i+1,0); + u[0]=1; + u[i]=1; + for (int j=1;j > re; + re=generate(5); + for (int i=0;i +using namespace std; + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x):val(x),left(NULL),right(NULL){} +}; + + void cur(TreeNode* root,int sum,int tsum,bool& re) + { + tsum+=root->val; + if (root->left==NULL&&root->right==NULL) + { + if (tsum==sum) + re=true; + return; + } + if (root->left!=NULL) + cur(root->left,sum,tsum,re); + if (root->right!=NULL) + cur(root->right,sum,tsum,re); + } + bool hasPathSum(TreeNode* root, int sum) { + bool re=false; + if (root==NULL) + return re; + cur(root,sum,0,re); + return re; + } diff --git a/Path SumII.cpp b/Path SumII.cpp new file mode 100644 index 0000000..d998bdf --- /dev/null +++ b/Path SumII.cpp @@ -0,0 +1,69 @@ +#include +#include +using namespace std; + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x):val(x),left(NULL),right(NULL){} +}; + +void cur(vector >& re,vector& cur_re,TreeNode* t,int sum,int tsum) + { + if (t->left==NULL&&t->right==NULL) + { + if (tsum==sum) + { + re.push_back(cur_re); + return; + } + else + return; + } + if (t->left!=NULL) + { + cur_re.push_back(t->left->val); + cur(re,cur_re,t->left,sum,tsum+t->left->val); + cur_re.pop_back(); + } + if (t->right!=NULL) + { + cur_re.push_back(t->right->val); + cur(re,cur_re,t->right,sum,tsum+t->right->val); + cur_re.pop_back(); + } + } + vector > pathSum(TreeNode* root, int sum) { + vector > re; + vector cur_re; + if (root==NULL) + return re; + cur_re.push_back(root->val); + cur(re,cur_re,root,sum,root->val); + return re; + } + +int main() +{ + TreeNode* root=new TreeNode(5); + root->left=new TreeNode(4); + root->right=new TreeNode(8); + root->left->left=new TreeNode(11); + root->right->left=new TreeNode(13); + root->right->right=new TreeNode(4); + root->left->left->left=new TreeNode(7); + root->left->left->right=new TreeNode(2); + root->right->right->left=new TreeNode(5); + root->right->right->right=new TreeNode(1); + + vector > re; + re=pathSum(root,22); + for (int i=0;i +#include +using namespace std; + + int numSquares(int n) { + int sq[n+1]; + for (int i=0;isq[i]+1) + sq[i+j*j]=sq[i]+1; + for (int i=0;i +#include +using namespace std; + +void cur(vector >& re,vector& cur_re,vector& left,int dep) +{ + if (dep==0) + { + re.push_back(cur_re); + return; + } + for (int i=0;i > permute(vector& nums) +{ + vector > re; + vector cur_re; + if (nums.size()==0) + return re; + cur(re,cur_re,nums,nums.size()); + return re; +} + +int main() +{ + vector nums; + nums.push_back(1); + nums.push_back(2); + nums.push_back(3); + vector > re; + re=permute(nums); + for (int i=0;i q; + q.push(root); + + while (!q.empty()) + { + int len = q.size(); + TreeLinkNode* t1; + TreeLinkNode* t2; + + for (int i=0;inext = t2; + } + if (t1->left!=NULL) + q.push(t1->left); + if (t1->right!=NULL) + q.push(t1->right); + } + t1->next = NULL; + } +} + diff --git a/Power of Two.cpp b/Power of Two.cpp new file mode 100644 index 0000000..8cd3af6 --- /dev/null +++ b/Power of Two.cpp @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main() +{ + bool re; + int n=9; + re=!(n&(n-1)); + cout< +#include +using namespace std; + +vector productExceptSelf(vector& nums) { + int len=nums.size(); + vector re; + if (len==2) + { + re.push_back(nums[1]); + re.push_back(nums[0]); + return re; + } + re.push_back(1); + int p=1; + + for (int i=1;i=0;i--) + { + p*=nums[i+1]; + re[i]*=p; + } + return re; + } + +int main() +{ + vector nums; + nums.push_back(9); + nums.push_back(0); + nums.push_back(-2); + vector re; + re=productExceptSelf(nums); + for (int i=0;ileft) + cur(t->left); + if (pre->val>t->val) + if (mistake1==NULL) + { + mistake1=pre; + post = t; + } + else + mistake2=t; + pre=t; + if (t->right) + cur(t->right); + +} + +void recoverTree(TreeNode* root) +{ + if (root==NULL) + return; + TreeNode* t = root; + while (t->left) + t=t->left; + pre=t; + cur(root); + int tmp=mistake1->val; + if (mistake2) + { + mistake1->val=mistake2->val; + mistake2->val=tmp; + } + else + { + mistake1->val=post->val; + post->val=tmp; + } +} + +int main() +{ + ifstream f("in.txt"); + string s; + vector ss; + while (!f.eof()) + { + f>>s; + ss.push_back(s); + } + TreeNode* t = buildtree(ss); + recoverTree(t); + printtree(t); + return 0; +} diff --git a/Rectangle Area.cpp b/Rectangle Area.cpp new file mode 100644 index 0000000..3a43822 --- /dev/null +++ b/Rectangle Area.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace std; + + int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { + int area1=(C-A)*(D-B); + int area2=(G-E)*(H-F); + + vector t1; + vector t2; + + t1.push_back(A); + t1.push_back(C); + t1.push_back(E); + t1.push_back(G); + + t2.push_back(B); + t2.push_back(D); + t2.push_back(F); + t2.push_back(H); + + + sort(t1.begin(),t1.end()); + sort(t2.begin(),t2.end()); + + int area=(t1[2]-t1[1])*(t2[2]-t2[1]); + return area1+area2-area; + } +int main +{ + +} diff --git a/Remove Duplicates from Sorted LIst.cpp b/Remove Duplicates from Sorted LIst.cpp new file mode 100644 index 0000000..044c74a --- /dev/null +++ b/Remove Duplicates from Sorted LIst.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +struct ListNode{ + int val; + ListNode* next; + ListNode(int x):val(x),next(NULL){} +}; + +ListNode* deleteDuplicates(ListNode* head) { + ListNode* tmp; + ListNode* re=head; + if (head==NULL) + return head; + while (head->next!=NULL) + { + if (head->val==head->next->val) + { + tmp=head->next; + head->next=head->next->next; + delete tmp; + } + else + { + head=head->next; + } + } + return re; + } + +int main() +{ + ListNode* head=new ListNode(1); + head->next=new ListNode(1); + ListNode* re=deleteDuplicates(head); + cout<<99; + while (re!=NULL) + { + cout<val<next; + } + return 0; +} diff --git a/Remove Element.cpp b/Remove Element.cpp new file mode 100644 index 0000000..0a95976 --- /dev/null +++ b/Remove Element.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +using namespace std; + +int removeElement(vector& nums, int val) { + sort(nums.begin(),nums.end()); + int len=nums.size(); + + if (len==0) + return 0; + if (len==1) + { + if (nums[0]==val) + return 0; + else + return 1; + } + + int high=len-1,low=0,mid=(high+low)/2; + while (lowval) + { + high=mid-1; + mid=(low+high)/2; + + } + else if (nums[mid]len-1) + p2=len-1; + while (true) + { + if (p1>=0) + { + if (nums[p1]==val) + { + nums.erase(nums.begin()+p1); + if (p1-1>=0) + p1--; + if (p2-1>=0) + p2--; + len--; + if (len==0) + break; + } + } + if (p2=len) + p2=len-1; + } + } + if (nums[p1]!=val&&nums[p2]!=val) + break; + } + + return nums.size(); + } + +int main() +{ + vector num; + num.push_back(4); + num.push_back(5); + num.push_back(5); + int i=removeElement(num,5); + cout< +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode* h=new ListNode(0); + h->next=head; + ListNode* front=head; + ListNode* back=h; + + for (int i=0;inext; + } + while (front->next!=NULL) + { + front=front->next; + back=back->next; + } + + if (back->next->next==NULL) + { + if (head==back->next) + head=head->next; + back->next=NULL; + return head; + } + if (back->next==head) + { + head=head->next; + return head; + } + ListNode* tmp=back->next; + back->next=back->next->next; + delete tmp; + return head; + } + +int main() +{ + ListNode* head=new ListNode(1); + + ListNode* h=removeNthFromEnd(head,1); + if(h==NULL) + cout<<99; + return 0; +} diff --git a/Reverse Linked List.cpp b/Reverse Linked List.cpp new file mode 100644 index 0000000..6edbc2b --- /dev/null +++ b/Reverse Linked List.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} + }; + +ListNode* reverseList(ListNode* head) { + if (head==NULL) + return head; + if (head->next==NULL) + return head; + ListNode* tmp=reverseList(head->next); + ListNode* tmpt=tmp; + while (tmpt->next!=NULL) + { + tmpt=tmpt->next; + } + tmpt->next=head; + head->next=NULL; + return tmp; + } + + int main() + { + ListNode* head=new ListNode(1); + head->next=new ListNode(2); + head->next->next=new ListNode(3); + head->next->next->next=new ListNode(4); + ListNode* h=reverseList(head); + cout<val<next->val<next->next->val<next->next->next->val; + return 0; + } diff --git a/Reverse Words in a String II.cpp b/Reverse Words in a String II.cpp new file mode 100644 index 0000000..818507b --- /dev/null +++ b/Reverse Words in a String II.cpp @@ -0,0 +1,25 @@ +#include "pack.h" + +void reverseWords(string &s) +{ + int len = s.size(); + vector pos; + pos.push_back(-1); + + for (int i=0;i q; + stringstream ss; + q.push(root); + string s; + + if (root==NULL) + return s; + + while (!q.empty()) + { + TreeNode* tmpnode = q.front(); + string tmpstring; + if (tmpnode != NULL) + { + tmpstring = inttostring(tmpnode->val); + s = s + tmpstring + " "; + } + else + { + s = s + "# "; + } + q.pop(); + if (tmpnode!=NULL) + { + q.push(tmpnode->left); + q.push(tmpnode->right); + } + } + s.erase(s.end()-1); + while (s[s.size()-1]=='#') + { + s.erase(s.end()-1); + s.erase(s.end()-1); + } + return s; +} + +TreeNode* deserialize(string data) +{ + if (data.size()==0) + return NULL; + queue q; + istringstream ss(data); + vector sv; + + int tmp; + do + { + string s; + ss>>s; + sv.push_back(s); + } while(ss); + TreeNode* root = new TreeNode(stringtoint(sv[0])); + q.push(root); + int k=0; + while (kleft = new TreeNode(tmp); + q.push(tm->left); + } + if (k+1right = new TreeNode(tmp); + q.push(tm->right); + } + + } + return root; +} + +int main() +{ + ifstream f("in.txt"); + string s; + vector ss; + while (!f.eof()) + { + f>>s; + ss.push_back(s); + } + TreeNode* t = buildtree(ss); + string sss = serialize(t); + //cout<& words, string word1, string word2) { + int loca1=-1; + int loca2=-1; + int dis = words.size()-1; + + for (int i=0;i=0) + dis = loca1 - loca2; + } + if (words[i]==word2) + { + loca2 = i; + if (i - loca1 < dis&& loca1>=0) + dis = loca2 - loca1; + } + cout< words; + words.push_back("a"); + words.push_back("b"); + words.push_back("c"); + words.push_back("d"); + words.push_back("d"); + int dis = shortestDistance(words,"a","d"); + cout< + + int myAtoi(string str) { + int len = str.size(); + if (len==0) return 0; + long long ans = 0; + int sign = 1; + int l = 0; + while (str[l]==' ') l++; + if (str[l]=='-') sign=-1,l++; + else if (str[l]=='+') l++; + for (int i=l;i=0&&str[i]-'0'<=9) + ans = ans*10 + (str[i] - '0'); + else + break; + } + ans = ans*sign; + if (ans>INT_MAX) + return INT_MAX; + if (ans +#include +using namespace std; + +void cur(vector >& re,vector& cur_re,vector nums,int left,int ss) +{ + if (left==0) + { + re.push_back(cur_re); + return; + } + for (int i=ss;i<=nums.size()-left;i++) + { + cur_re.push_back(nums[i]); + cur(re,cur_re,nums,left-1,i+1); + cur_re.pop_back(); + while (nums[i]==nums[i+1]) {i++;} + } +} +vector > subsets(vector& nums) +{ + vector cur_re; + vector > re; + re.push_back(cur_re); + if (nums.size()==0) + return re; + + for (int i=0;i > re; + vector cur_re; + vector num; + num.push_back(1); + num.push_back(1); + num.push_back(2); + num.push_back(2); + re=subsets(num); + for (int i=0;i +#include +using namespace std; + +struct TreeNode +{ + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x):val(x),left(NULL),right(NULL){} +}; + +void sum_cur(vector >& re,vector& cur,TreeNode* t) +{ + cur.push_back(t->val); + if (t->left == NULL && t->right == NULL) + { + re.push_back(cur); + return; + } + if (t->left != NULL) + { + sum_cur(re,cur,t->left); + cur.pop_back(); + } + if (t->right != NULL) + { + sum_cur(re,cur,t->right); + cur.pop_back(); + } + return; +} + +int sumNumbers(TreeNode* root) +{ + vector > re; + vector cur; + if (root == NULL) + return 0; + sum_cur(re,cur,root); + + int sum = 0; + for (int i = 0; i < re.size(); i++) + { + int tmpsum = re[i][0]; + for (int j = 1; j < re[i].size(); j++) + {tmpsum = tmpsum * 10 + re[i][j];} + sum += tmpsum; + } + return sum; +} + +int main() +{ + TreeNode* t = new TreeNode(4); + t->left = new TreeNode(9); + t->right = new TreeNode(0); + t->right->left = new TreeNode(2); + t->left->right = new TreeNode(1); + + int sum = sumNumbers(t); + cout< +#include +#include +#include +using namespace std; + +string inttostring(int num) +{ + stringstream ss; + string s; + ss<>s; + return s; +} + +vector summaryRanges(vector& nums) +{ + vector re; + int len = nums.size(); + string s; + int k = 0; + + if (len==0) + return re; + if (len==1) + { + s = inttostring(nums[0]); + re.push_back(s); + return re; + } + s = inttostring(nums[k]); + while (k0) + s = s + "->" + inttostring(nums[k]); + k++; + re.push_back(s); + if (k == len) + return re; + s = inttostring(nums[k]); + if (k == len-1) + { + re.push_back(s); + return re; + } + } +} + +int main() +{ + vector nums; + nums.push_back(0); + nums.push_back(1);nums.push_back(2);nums.push_back(4);nums.push_back(7); + vector re = summaryRanges(nums); + for (int i=0;i +using namespace std; + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x):val(x),left(NULL),right(NULL){} +}; diff --git a/Ugly Number.cpp b/Ugly Number.cpp new file mode 100644 index 0000000..9f4bf60 --- /dev/null +++ b/Ugly Number.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + + bool isUgly(int num) { + if (num==1) + return true; + if (num==0) + return false; + while (num%2==0) + { + num/=2; + } + while (num%3==0) + { + num/=3; + } + while (num%5==0) + { + num/=5; + } + if (num==1) + return true; + else + return false; + } + +int main() +{ + int a=17; + bool b=isUgly(a); + cout< +using namespace std; + + int cur(int sleft,int eleft,int sright,int eright) + { + int contl,contr; + if (sleft>eleft&&sright>eright) + return 1; + if (sleft<=eleft) + { + contl=0; + for (int i=sleft;i<=eleft;i++) + { + contl+=cur(sleft,i-1,i+1,eleft); + } + } + else + contl=1; + if (sright<=eright) + { + contr=0; + for (int i=sright;i<=eright;i++) + { + contr+=cur(sright,i-1,i+1,eright); + + } + } + else + contr=1; + return contr*contl; + } + int numTrees(int n) { + int cont=0; + for (int i=1;i<=n;i++) + { + cont+=cur(1,i-1,i+1,n); + } + return cont; + } + +int main() +{ + int num; + num=numTrees(3); + cout< +using namespace std; + +int uniquePaths(int m, int n) { + double C=1; + for (int i=0;iint(C)) + b=int(C)+1; + else + b=int(C); + return b; + } + +int main() +{ + int re=uniquePaths(56,5); + cout< maplist; + ValidWordAbbr(vector &dictionary) + { + int len = dictionary.size(); + for (int i=0;i(tmp,dictionary[i])); + } + } + string generateabbr(string s) + { + int len = s.size(); + string re; + if (len <= 2) + return s; + re.push_back(s[0]); + re = re + to_String(len-2); + re.push_back(s[len-1]); + return re; + } + bool isUnique(string word) + { + string tmp = generateabbr(word); + if (map.count(tmp)) + + return 0; + else + return 1; + } +}; + +int main() +{ + +} diff --git a/Valid Palindrome.cpp b/Valid Palindrome.cpp new file mode 100644 index 0000000..61b0ed0 --- /dev/null +++ b/Valid Palindrome.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +bool isPalindrome(string s) { + int len=s.size(); + for (int i=0;i='0'&&s[i]<='9')||(s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')) + i++; + else + { + s.erase(s.begin()+i); + len--; + } + } + if (s.size()==0) + return true; + + int p1=0,p2=s.size()-1; + while (p1<=p2) + { + if (s[p1]==s[p2] || (s[p1]-'a')==(s[p2]-'A') || (s[p1]-'A')==(s[p2]-'a')) + { + p1++; + p2--; + } + else + { + return false; + } + } + return true; + } + +int main() +{ + string a="Bab"; + bool b=isPalindrome(a); + cout< +#include +#include +using namespace std; + +bool isValid(string s) { + vector judg; + judg.push_back(s[0]); + + for (int i=1;i j; + string s="{[]}(}}"; + bool c=isValid(s); + cout<left,pre); + if(pre!=NULL){ + if (pre->val>=p->val){ + valid=false; + return;}} + pre=p; + inorder(p->right,pre); +} + + bool isValidBST(TreeNode* root) { + valid=true; + TreeNode* pre=NULL; + inorder(root,pre); + return valid; + } + +int main() +{ + TreeNode* t = new TreeNode(1); + t->left = new TreeNode(1); + bool a = isValidBST(t); + cout< q; + q.push(root); + int maxvalue = root->val; + while(!q.empty()) + { + TreeNode* tmp = q.front(); + if (tmp->val>maxvalue) maxvalue = tmp->val; + if (tmp->left!=NULL) q.push(tmp->left); + if (tmp->right!=NULL) q.push(tmp->right); + q.pop(); + } + return maxvalue; +} + +int findmin(TreeNode* root) +{ + queue q; + q.push(root); + int minvalue = root->val; + while(!q.empty()) + { + TreeNode* tmp = q.front(); + if (tmp->valval; + if (tmp->left!=NULL) q.push(tmp->left); + if (tmp->right!=NULL) q.push(tmp->right); + q.pop(); + } + return minvalue; +} + +/*bool isValidBST(TreeNode* root) +{ + if (root==NULL) return true; + int leftmax,rightmin; + if (root->left!=NULL) + { + leftmax = findmax(root->left); + if (leftmax>=root->val) + return false; + } + if (root->right!=NULL) + { + rightmin = findmin(root->right); + if (rightmin<=root->val) + return false; + } + if (!isValidBST(root->left)) + return false; + if (!isValidBST(root->right)) + return false; + return true; +}*/ +bool check(TreeNode *node, int leftVal, int rightVal) + { + if (node == NULL) + return true; + + return leftVal < node->val && node->val < rightVal && check(node->left, leftVal, node->val) && + check(node->right, node->val, rightVal); + } + + bool isValidBST(TreeNode *root) { + // Start typing your C/C++ solution below + // DO NOT write int main() function + return check(root, INT_MIN, INT_MAX); + } + +int main() +{ + ifstream f("in.txt"); + string s; + vector ss; + while (!f.eof()) + { + f>>s; + ss.push_back(s); + } + TreeNode* t = buildtree(ss); + bool out = isValidBST(t); + cout< >& board,string word) +{ + bool re = false; + for (int i = 0;i >& board,string& word,int k,int i,int j,bool& re) +{ + int h = board.size(); + int w = board[0].size(); + if (k==word.size()) + { + re = true; + return; + } + if (board[i][j]==word[k]) + { + if (i+1=0) + searchword(board,word,k+1,i-1,j,re); + if (j-1>=0) + searchword(board,word,k+1,i,j-1,re); + } +} diff --git a/largestNumber.cpp b/largestNumber.cpp new file mode 100644 index 0000000..40d4949 --- /dev/null +++ b/largestNumber.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +static bool cmp(const string a, const string b) + { + return (a+b)>(b+a); + } + string largestNumber(vector &num) { + int len=num.size(); + vector vec; + + if (len==0) + return NULL; + + int zeroc=0; + for (int i=0;i a; + a.push_back(3); + a.push_back(1); + a.push_back(2); + a.push_back(5); + + sort(a); + + return 0; +} diff --git a/link.cpp b/link.cpp new file mode 100644 index 0000000..900fe8c --- /dev/null +++ b/link.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} + }; + +ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + ListNode* p1=l1; + ListNode* p2=l2; + + if (p1==NULL && p2==NULL) + { + return NULL; + } + else if (p1==NULL && p2!=NULL) + { + return p2; + } + else if (p1!=NULL && p2==NULL) + { + return p1; + } + + if (p2->valval) + { + ListNode* tt=p2->next; + p2->next=p1; + p1=p2; + p2=tt; + } + l1=p1; + + while (p2!=NULL) + { + if (p1->next==NULL) + { + p1->next=p2; + return l1; + } + if (p1->val<=p2->val && p1->next->val>p2->val) + { + ListNode* tmp=p2->next; + p2->next=p1->next; + p1->next=p2; + p2=tmp; + } + p1=p1->next; + } + return l1; + } + + int main() + { + ListNode* l1=new ListNode(5); + ListNode* l2=new ListNode(1); + l2->next=new ListNode(2); + l2->next->next=new ListNode(4); + ListNode* l=mergeTwoLists(l1,l2); + while (l!=NULL){ + cout<val<next;} + return 0; + } diff --git a/map.cpp b/map.cpp new file mode 100644 index 0000000..e69de29 diff --git a/mergesort.cpp b/mergesort.cpp new file mode 100644 index 0000000..f5ef14a --- /dev/null +++ b/mergesort.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +void merger(int a[],int low,int mid,int high) +{ + int tmp[high-low+1]; + int i=low, j=mid, k=0; + + while (i +#include +using namespace std; + +//string multiply(string num1, string num2) { +// int len1=num1.size(); +// int len2=num2.size(); +// +// int len=len1+len2; +// int s[len]; +// +// for (int i=0;i=10) +// { +// int k=m+n+1; +// int tm=tmp-tmp/10*10; +// int tm2=tmp/10; +// if (s[m+n]+tm>=10) +// { +// s[m+n]=s[m+n]+tm-10; +// tm2++; +// } +// else +// { +// s[m+n]+=tm; +// } +// if (s[m+n+1]+tm2>=10) +// { +// +// s[m+n+1]=s[m+n+1]+tm2-10; +// while (s[k+1]==9 && k+1=10) +// { +// int k=m+n; +// s[m+n]=s[m+n]+tmp-10; +// while (s[k+1]==9 && k+1=0 && len2 >=0) + { + int a = num1[len1]-'0'; + int b = num2[len2]-'0'; + int c = a+b; + if(carry) + { + c+=1; + carry=false; + } + if(c>9) + { + carry=true; + c -= 10; + } + char tmp = '0'+c; + res = tmp + res; + len1--; + len2--; + } + string pre=""; + if(len1>=0) + pre=num1.substr(0,len1+1); + else if(len2>=0) + pre=num2.substr(0,len2+1); + if(carry){ + int sz=pre.size()-1; + while(sz>=0 && pre[sz]=='9') + { + pre[sz]='0'; + sz--; + } + if(sz<0) + pre='1'+pre; + else + pre[sz]+=1; + } + res = pre+res; + return res; + } + string multiplyOneBit(string num1, int num2) + { + int len = num1.size(); + int* res = new int[len+1]; + memset(res,0,sizeof(int)*(len+1)); + + for(int i=len;i>0;i--) + { + res[i] += (num1[i-1]-'0')*num2; + res[i-1] += res[i]/10; + res[i] %= 10; + } + string ss=""; + for(int i=0; i<=len; i++) + ss += char('0' + res[i]); + return ss; + } + string multiply(string num1, string num2) { + // Note: The Solution object is instantiated only once. + if(num1=="0" || num2=="0") return "0"; + int l1 = num1.length(), l2 = num2.length(); + string res=""; + for(int i =0; i< l2;i++) + { + res+='0'; + string tmp = multiplyOneBit(num1,num2[i]-'0'); + res = addTwoString(res,tmp); + } + res = res[0]=='0'? res.substr(1):res; + return res; + } + +int main() +{ + + string s1="7950730684116360645035469888615500796"; + string s2="841979251126152713721537069775651940998000937338602066982"; + string s=multiply(s1,s2); + cout<<"a"+'b'; + return 0; +} diff --git a/pack.h b/pack.h new file mode 100644 index 0000000..44ba6a6 --- /dev/null +++ b/pack.h @@ -0,0 +1,16 @@ +#ifndef PACK_H_INCLUDED +#define PACK_H_INCLUDED + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#endif // PACK_H_INCLUDED diff --git a/palindrome.cpp b/palindrome.cpp new file mode 100644 index 0000000..3c13d78 --- /dev/null +++ b/palindrome.cpp @@ -0,0 +1,56 @@ +#include +#include +using namespace std; + +string longestPalindrome(string s) { + int len=s.size(); + int relen=0; + string re=""; + if (len==1) + return s; + + for (int i=1;i=0 && jrelen) + { + re.assign(s,2*i-j,(j-i)*2); + relen=(j-i)*2; + } + } + + if (s[i-1]==s[i+1]) + { + int j=i+1; + while (j0) + { + if (s[j]!=s[2*i-j]) + break; + j++;cout<relen) + { + re.assign(s,2*i-j+1,(j-i)*2-1); + relen=(j-i)*2-1; + } + } + + } + return re; + } + +int main() +{ + string s="aaa"; + string re=longestPalindrome(s); + cout< +#include + +using namespace std; + +void sortColors(vector& nums) { + int len=nums.size(); + + for (int i=0;i0 && nums[k]!=0) + { + k--; + } + int tmp=nums[k+1]; + nums[k+1]=nums[i]; + nums[i]=tmp; + } + } + } + +int main() +{ + vector a; + a.push_back(9); + a[0]=8; + cout< +#include +#include +using namespace std; + +int main() +{ + vector a; + cout<<'1'-48; + return 0; +} diff --git a/string.cpp b/string.cpp new file mode 100644 index 0000000..dc33842 --- /dev/null +++ b/string.cpp @@ -0,0 +1,56 @@ +#include +#include +using namespace std; + +struct Node +{ + int elem; + int loca; + Node* next; + Node(int e,int l):elem(e),loca(l),next(NULL){} +}; + +int lengthOfLongestSubstring(string s) +{ + Node* ele[26]; + Node* p[26]; + Node* temp[26]; + int len=s.size(); + + for (int i=0;i<26;i++) + { + ele[i]=new Node(i+97,0); + p[i]=ele[i]->next; + temp[i]=ele[i]->next; + } + + for (int i=0;inext; + } + + int dif=len; + for (int i=0;i<26;i++) + { + while (temp[i]->next!=NULL) + { + int tmp=temp[i+1]->loca-temp[i]->loca; + if (tmpnext; + } + } + return dif; + +} + +int main() +{ + string s="abcabcbb"; + int p=lengthOfLongestSubstring(s); + cout< +#include diff --git a/vector.cpp b/vector.cpp new file mode 100644 index 0000000..4a3a0bc --- /dev/null +++ b/vector.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +using namespace std; + +void sxun(vector &out, string digits, int arr[], int loca) +{ + vector p; + int pn[8]={3,3,3,3,3,4,3,4}; + p.push_back("abc"); + p.push_back("def"); + p.push_back("ghi"); + p.push_back("jkl"); + p.push_back("mno"); + p.push_back("pqrs"); + p.push_back("tuv"); + p.push_back("wxyz"); + int len=array.size(); + char* s; + if (loca==len-1) + { + for (int j=0;j letterCombinations(string digits) +{ + vector p; + vector out; + int len=digits.size(); + int nums[len]; + + int pn[8]={3,3,3,3,3,4,3,4}; + p.push_back("abc"); + p.push_back("def"); + p.push_back("ghi"); + p.push_back("jkl"); + p.push_back("mno"); + p.push_back("pqrs"); + p.push_back("tuv"); + p.push_back("wxyz"); + + for (int i=0;i +#include +#include +#include +using namespace std; + +vector maxSlidingWindow(vector& nums, int k) { + vector out; + vector win; + + if (nums.size()==0) + return win; + + int tmpmax=nums[0]; + int loca; + + for (int i=0;itmpmax) + { + tmpmax=nums[i]; + loca=i; + } + } + + out.push_back(tmpmax); + + for (int i=1;itmpmax) + { + tmpmax=nums[i+k-1]; + loca=i+k-1; + out.push_back(tmpmax); + continue; + } + else if (nums[i+k-1]tmpmax) + { + tmpmax=nums[i+j]; + loca=i+j; + } + } + out.push_back(tmpmax); + } + else + { + out.push_back(tmpmax); + } + } + } + return out; + } + +int main() +{ + vector a; + maxSlidingWindow(a,0); + return 0; +}