Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stonre committed Mar 21, 2016
0 parents commit 5ddc476
Show file tree
Hide file tree
Showing 100 changed files with 4,182 additions and 0 deletions.
63 changes: 63 additions & 0 deletions 15.3Sum1.cpp
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 added 3summ.cpp
Empty file.
55 changes: 55 additions & 0 deletions Add Binary.cpp
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;
}
25 changes: 25 additions & 0 deletions Add Digits.cpp
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;
}
51 changes: 51 additions & 0 deletions Balanced Binary Tree.cpp
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;
}
35 changes: 35 additions & 0 deletions Best Time to Buy and Sell Stock II.cpp
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;
}
19 changes: 19 additions & 0 deletions Best Time to Buy and Sell Stock with Cooldown.cpp
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];
}
39 changes: 39 additions & 0 deletions Best Time to Buy and Sell Stock.cpp
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;
}
34 changes: 34 additions & 0 deletions Binary Search Tree Iterator.cpp
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;
}
};
42 changes: 42 additions & 0 deletions Binary Tree Inorder Traversal.cpp
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;
}
Loading

0 comments on commit 5ddc476

Please sign in to comment.