-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5ddc476
Showing
100 changed files
with
4,182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <fstream> | ||
#include <map> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
vector<vector<int> > threeSum(vector<int>& nums) | ||
{ | ||
vector<vector<int> > result; | ||
if (nums.size()<3) return result; | ||
int target=0; | ||
sort(nums.begin(),nums.end()); | ||
for (int head=0;head<nums.size();head++) | ||
{ | ||
while(head!=0&&nums[head]==nums[head-1]) head++; | ||
target=0; | ||
vector<int> one; | ||
one.push_back(nums[head]); | ||
target=target-nums[head]; | ||
map <int,int> nummap; | ||
int flag=nums[nums.size()-1]+1; | ||
for (int i=head+1;i<nums.size();i++) | ||
{ | ||
if (nummap.count(target-nums[i])&&nums[i]!=flag) | ||
{ | ||
flag=nums[i]; | ||
one.push_back(nums[nummap[target-nums[i]]]); | ||
one.push_back(nums[i]); | ||
result.push_back(one); | ||
one.pop_back(); | ||
one.pop_back(); | ||
} | ||
if (!nummap.count(nums[i])) | ||
{ | ||
nummap.insert(pair<int,int>(nums[i],i)); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
int main() | ||
{ | ||
ifstream fin("in.txt"); | ||
vector <int> nums; | ||
int num_tmp; | ||
while(!fin.eof()) | ||
{ | ||
fin>>num_tmp; | ||
nums.push_back(num_tmp); | ||
} | ||
// for(int i=0;i<nums.size();i++) cout<<nums[i]<<' '; | ||
cout<<endl; | ||
vector<vector<int> > result=threeSum(nums); | ||
for(int i=0;i<result.size();i++) | ||
{ | ||
for(int j=0;j<result[i].size();j++) cout<<result[i][j]<<' '; | ||
cout<<endl; | ||
} | ||
return 0; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include<iostream> | ||
#include<cstring> | ||
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;i<min(lena,lenb);i++) | ||
{ | ||
re[max(lena,lenb)-i]=a[lena-1-i]-'0'+b[lenb-1-i]-'0'; | ||
} | ||
i--; | ||
while (i<lenb-1){ | ||
i++; | ||
re[max(lena,lenb)-i]=b[lenb-1-i]-'0'; | ||
} | ||
while (i<lena-1){ | ||
i++; | ||
re[max(lena,lenb)-i]=a[lena-1-i]-'0'; | ||
} | ||
|
||
for (int j=max(lena,lenb);j>0;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<<c; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include<iostream> | ||
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<<c; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include<iostream> | ||
#include<cmath> | ||
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<<isB; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
int maxProfit(vector<int>& 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<int> nums; | ||
nums.push_back(1); | ||
nums.push_back(2); | ||
cout<<maxProfit(nums); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "pack.h" | ||
|
||
int maxProfit(vector<int>& 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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
int maxProfit(vector<int>& 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<int> nums; | ||
nums.push_back(2); | ||
nums.push_back(1); | ||
nums.push_back(4); | ||
int re = maxProfit(nums); | ||
cout<<re; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "tree.h" | ||
|
||
class BSTIterator | ||
{ | ||
public: | ||
stack<TreeNode*> 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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include<iostream> | ||
#include<vector> | ||
using namespace std; | ||
|
||
struct TreeNode{ | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode(int x):val(x),left(NULL),right(NULL){} | ||
}; | ||
|
||
void cur(vector<int>& 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<int> inorderTraversal(TreeNode* root) { | ||
vector<int> 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<int> re; | ||
re=inorderTraversal(root); | ||
for (int i=0;i<re.size();i++) | ||
{ | ||
cout<<re[i]<<" "; | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.