Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyl committed Dec 13, 2012
1 parent f4d3a7f commit 8479e70
Show file tree
Hide file tree
Showing 42 changed files with 1,415 additions and 4 deletions.
36 changes: 36 additions & 0 deletions 3Sum Closest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(num.begin(), num.end());
int len = num.size();
int tmp = 3 * num[len-1] + abs(target) + 1;
int result = 0;
for (int i = 0; i != len; i++){
int j = 0, k = len - 1;
while(j < k){
if (j == i){
j++;
continue;
}else if (k == i){
k--;
continue;
}
int sum = num[i] + num[j] + num[k];
if (abs(sum-target) < tmp){
result = sum;
tmp = abs(sum-target);
}
if (sum > target)
k--;
else if (sum < target)
j++;
else{
return result;
}
}
}
return result;
}
};
32 changes: 32 additions & 0 deletions 3Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > result;
vector<int> tmp;
sort(num.begin(), num.end());
if (num.size() < 3)
return result;
for (int i = 0; i != num.size() - 2; i++){
if (num[i] > 0)
break;
for (int j = i+1; j != num.size() - 1; j++){
if (num[i] + num[j] > 0)
break;
int n = 0 - num[i] - num[j];
if(binary_search(num.begin()+j+1, num.end(), n)){
tmp.clear();
tmp.push_back(num[i]);
tmp.push_back(num[j]);
tmp.push_back(n);
result.push_back(tmp);
}
}
}
sort(result.begin(), result.end());
vector<vector<int> >::iterator iter = unique(result.begin(), result.end());
result.erase(iter, result.end());
return result;
}
};
54 changes: 54 additions & 0 deletions 4Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(num.begin(), num.end());
int len = num.size();
vector<vector<int> > result;
if (len < 4)
return result;
vector<int> tmp(4);

for (int i = 0; i != len - 1; i++){
for (int j = i+1; j != len; j++){
int tar = target - num[i] - num[j];
tmp[0] = num[i];
tmp[1] = num[j];
int p = 0, q = len - 1;
while(p < q){
if (p == i || p == j){
p++;
continue;
}
if (q == i || q == j){
q--;
continue;
}

int sum = num[p] + num[q];
if (sum == tar){
tmp[2] = num[p];
tmp[3] = num[q];
result.push_back(tmp);
p++;
q--;
continue;
}else if (sum > tar)
q--;
else if (sum < tar)
p++;
}
}
}
for (int i = 0; i != result.size(); i++){
sort(result[i].begin(), result[i].end());
}
sort(result.begin(), result.end());
vector<vector<int> >::iterator iter = unique(result.begin(), result.end());
result.erase(iter, result.end());
return result;

}
};
37 changes: 37 additions & 0 deletions Add Binary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public:
string addBinary(string a, string b) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int t = 0;
int lena = a.size();
int lenb = b.size();

reverse(a);
reverse(b);

string str1, str2;
string result = "";
int len1 = lena > lenb ? lena : lenb; // the shorter one
int len2 = lena > lenb ? lenb : lena;
str1 = lena > lenb ? a : b;
str2 = lena > lenb ? b : a;

for (int i = 0; i != len2; i++){
int x = str1[i] - '0' + str2[i] - '0' + t;
t = x/2;
result = result + (char)(x%2 + '0');
}
for (int i = len2; i != len1; i++){
int x = str1[i] - '0' + t;
t = x / 2;
result = result + (char)(x%2 + '0');
}
if (t)
result = result + '1';

reverse(result);
return result;

}
};
38 changes: 38 additions & 0 deletions Add Two Numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function

vector<vector<int> > result;
int len = candidates.size();
if (target <= 0 || len == 0){
return result;
}


for (int i = 0; i != len; i++){
vector<int> tmp;
if (candidates[i] == target){
tmp.push_back(candidates[i]);
result.push_back(tmp);
}else{
vector<vector<int> > res = combinationSum(candidates, target - candidates[i]);
for (int j = 0; j != res.size(); j++){
res[j].push_back(candidates[i]);
result.push_back(res[j]);
}

}
}

for (int i = 0; i != result.size(); i++){
sort(result[i].begin(), result[i].end());
}

sort(result.begin(), result.end());
vector<vector<int> >::iterator ite = unique(result.begin(), result.end());
result.erase(ite, result.end());
return result;
}
};
30 changes: 30 additions & 0 deletions Balanced Binary Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
bool flag = true;
int height(TreeNode *root){
if (root == NULL)
return 0;
int left = height(root->left);
int right = height(root->right);
if (abs(right-left) > 1)
flag = false;
return max(right, left) + 1;
}

class Solution {
public:
bool isBalanced(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
flag = true;
height(root);
return flag;
}
};
27 changes: 27 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,27 @@
class Solution {
public:
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (prices.size() < 2)
return 0;

int cur = prices[0];
bool own = false;
if (cur <= prices[1])
own = true;
int result = 0;

for (int i = 1; i != prices.size() - 1; i++){
if (!own && prices[i] <= prices[i-1] && prices[i] <= prices[i+1])
cur = prices[i], own=true;
if (own && prices[i] >= prices[i-1] && prices[i] >= prices[i+1])
result += prices[i] - cur, own=false;
}
if (prices[prices.size() - 1] > prices[prices.size() - 2] && prices[prices.size() -1] > cur)
result += prices[prices.size() - 1] - cur;

return result;

}
};
21 changes: 21 additions & 0 deletions Best Time to Buy and Sell Stock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (prices.size() < 2)
return 0;

int result = 0, cur = prices[0];

for (int i = 1; i != prices.size(); i++){
if (prices[i] < cur){
cur = prices[i];
}
else if(prices[i] - cur > result)
result = prices[i] - cur;
}
return result;

}
};
30 changes: 30 additions & 0 deletions Binary Tree Inorder Traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/

vector<int> result;

void search(TreeNode *root){
if (!root)
return ;
search(root->left);
result.push_back(root->val);
search(root->right);
}

class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
result.clear();
search(root);
return result;
}
};
38 changes: 38 additions & 0 deletions Binary Tree Level Order Traversal II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
vector<vector<int> > result;

void search(TreeNode* root, int height){
if (!root)
return ;
if (result.size() < height+1){
vector<int> tmp;
result.push_back(tmp);
}

search(root->left, height+1);
result[height].push_back(root->val);
search(root->right, height+1);
}

class Solution {
public:
vector<vector<int> > levelOrderBottom(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
result.clear();
search(root, 0);
vector<vector<int> > res;
for (int i = result.size() - 1; i != -1; i--){
res.push_back(result[i]);
}
return res;
}
};
35 changes: 35 additions & 0 deletions Binary Tree Level Order Traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/

vector<vector<int> > result;

void search(TreeNode* root, int height){
if (!root)
return ;
if (result.size() < height+1){
vector<int> tmp;
result.push_back(tmp);
}

search(root->left, height+1);
result[height].push_back(root->val);
search(root->right, height+1);
}

class Solution {
public:
vector<vector<int> > levelOrder(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
result.clear();
search(root, 0);
return result;
}
};
Loading

0 comments on commit 8479e70

Please sign in to comment.