Skip to content

Commit 5cb5aa3

Browse files
committed
leetcode solutions
0 parents  commit 5cb5aa3

File tree

153 files changed

+11325
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+11325
-0
lines changed

3Sum/3Sum.cpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Source : https://oj.leetcode.com/problems/3sum/
2+
// Author : Hao Chen
3+
// Date : 2014-07-22
4+
5+
#include <stdio.h>
6+
#include <iostream>
7+
#include <vector>
8+
#include <set>
9+
#include <algorithm>
10+
using namespace std;
11+
12+
13+
//solution: http://en.wikipedia.org/wiki/3SUM
14+
vector<vector<int> > threeSum(vector<int> &num) {
15+
16+
vector< vector<int> > result;
17+
18+
sort(num.begin(), num.end());
19+
20+
int n = num.size();
21+
22+
for (int i=0; i<n-2; i++) {
23+
//skip the duplication
24+
if (i>0 && num[i-1]==num[i]) continue;
25+
int a = num[i];
26+
int low = i+1;
27+
int high = n-1;
28+
while ( low < high ) {
29+
int b = num[low];
30+
int c = num[high];
31+
if (a+b+c == 0) {
32+
//got the soultion
33+
vector<int> v;
34+
v.push_back(a);
35+
v.push_back(b);
36+
v.push_back(c);
37+
result.push_back(v);
38+
// Continue search for all triplet combinations summing to zero.
39+
//skip the duplication
40+
while(low<n && num[low]==num[low+1]) low++;
41+
while(high>0 && num[high]==num[high-1]) high--;
42+
low++;
43+
high--;
44+
} else if (a+b+c > 0) {
45+
//skip the duplication
46+
while(high>0 && num[high]==num[high-1]) high--;
47+
high--;
48+
} else{
49+
//skip the duplication
50+
while(low<n && num[low]==num[low+1]) low++;
51+
low++;
52+
}
53+
}
54+
}
55+
return result;
56+
}
57+
58+
//using combination method could meet <<Time Limit Exceeded>> error
59+
vector<vector<int> > combination(vector<int> &v, int k);
60+
bool isSumZero(vector<int>& v);
61+
int sum(vector<int>& v);
62+
63+
vector<vector<int> > threeSum2(vector<int> &num) {
64+
vector< vector<int> > result;
65+
vector< vector<int> > r = combination(num, 3);
66+
for (int i=0; i<r.size(); i++){
67+
if (isSumZero(r[i])){
68+
result.push_back(r[i]);
69+
}
70+
}
71+
return result;
72+
}
73+
74+
bool isSumZero(vector<int>& v){
75+
return sum(v)==0;
76+
}
77+
78+
int sum(vector<int>& v){
79+
int s=0;
80+
for(int i=0; i<v.size(); i++){
81+
s += v[i];
82+
}
83+
return s;
84+
}
85+
86+
vector<vector<int> > combination(vector<int> &v, int k) {
87+
88+
vector<vector<int> > result;
89+
vector<int> d;
90+
int n = v.size();
91+
for (int i=0; i<n; i++){
92+
d.push_back( (i<k) ? 1 : 0 );
93+
}
94+
95+
//1) from the left, find the [1,0] pattern, change it to [0,1]
96+
//2) move all of the 1 before the pattern to the most left side
97+
//3) check all of 1 move to the right
98+
while(1){
99+
vector<int> tmp;
100+
for(int x=0; x<n; x++){
101+
if (d[x]) tmp.push_back(v[x]);
102+
}
103+
sort(tmp.begin(), tmp.end());
104+
result.push_back(tmp);
105+
//step 1), find [1,0] pattern
106+
int i;
107+
bool found = false;
108+
int ones =0;
109+
for(i=0; i<n-1; i++){
110+
111+
if (d[i]==1 && d[i+1]==0){
112+
d[i]=0; d[i+1]=1;
113+
found = true;
114+
//step 2) move all of right 1 to the most left side
115+
for (int j=0; j<i; j++){
116+
d[j]=( ones > 0 ) ? 1 : 0;
117+
ones--;
118+
}
119+
break;
120+
}
121+
if (d[i]==1) ones++;
122+
}
123+
if (!found){
124+
break;
125+
}
126+
127+
}
128+
return result;
129+
}
130+
131+
132+
void printMatrix(vector<vector<int> > &matrix)
133+
{
134+
for(int i=0; i<matrix.size(); i++){
135+
printf("{");
136+
for(int j=0; j< matrix[i].size(); j++) {
137+
printf("%3d ", matrix[i][j]) ;
138+
}
139+
printf("}\n");
140+
}
141+
cout << endl;
142+
}
143+
144+
145+
int main()
146+
{
147+
//int a[] = {-1, 0, 1, 2, -1, 1, -4};
148+
int a[] = {-1, 1, 1, 1, -1, -1, 0,0,0};
149+
vector<int> n(a, a+sizeof(a)/sizeof(int));
150+
vector< vector<int> > result = threeSum(n);
151+
printMatrix(result);
152+
return 0;
153+
}

3SumClosest/3SumClosest.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Source : https://oj.leetcode.com/problems/3sum-closest/
2+
// Author : Hao Chen
3+
// Date : 2014-07-03
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <iostream>
8+
#include <vector>
9+
#include <set>
10+
#include <algorithm>
11+
using namespace std;
12+
13+
#define INT_MAX 2147483647
14+
//solution: http://en.wikipedia.org/wiki/3SUM
15+
int threeSumClosest(vector<int> &num, int target) {
16+
17+
18+
sort(num.begin(), num.end());
19+
20+
int n = num.size();
21+
int distance = INT_MAX;
22+
int result;
23+
24+
for (int i=0; i<n-2; i++) {
25+
//skip the duplication
26+
if (i>0 && num[i-1]==num[i]) continue;
27+
int a = num[i];
28+
int low = i+1;
29+
int high = n-1;
30+
while ( low < high ) {
31+
int b = num[low];
32+
int c = num[high];
33+
int sum = a+b+c;
34+
if (sum - target == 0) {
35+
//got the final soultion
36+
return target;
37+
} else if (sum -target> 0) {
38+
if (abs(sum-target) < distance ) {
39+
distance = abs(sum - target);
40+
result = sum;
41+
}
42+
//skip the duplication
43+
while(high>0 && num[high]==num[high-1]) high--;
44+
high--;
45+
} else{
46+
if (abs(sum-target) < distance) {
47+
distance = abs(sum - target);
48+
result = sum;
49+
}
50+
//skip the duplication
51+
while(low<n && num[low]==num[low+1]) low++;
52+
low++;
53+
}
54+
}
55+
}
56+
return result;
57+
58+
}
59+
60+
61+
62+
63+
int main()
64+
{
65+
int a[] = {-1, 2, 1, -4};
66+
vector<int> n(a, a+sizeof(a)/sizeof(int));
67+
int target = 1;
68+
cout << threeSumClosest(n, target) << endl;
69+
return 0;
70+
}

4Sum/4Sum.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Source : https://oj.leetcode.com/problems/4sum/
2+
// Author : Hao Chen
3+
// Date : 2014-07-03
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
using namespace std;
9+
10+
vector<vector<int> > threeSum(vector<int> num, int target);
11+
12+
vector<vector<int> > fourSum(vector<int> &num, int target) {
13+
vector< vector<int> > result;
14+
if (num.size()<4) return result;
15+
sort( num.begin(), num.end() );
16+
17+
for(int i=0; i<num.size()-3; i++) {
18+
//skip the duplication
19+
if (i>0 && num[i-1]==num[i]) continue;
20+
vector<int> n(num.begin()+i+1, num.end());
21+
vector<vector<int> > ret = threeSum(n, target-num[i]);
22+
for(int j=0; j<ret.size(); j++){
23+
ret[j].insert(ret[j].begin(), num[i]);
24+
result.push_back(ret[j]);
25+
}
26+
}
27+
28+
return result;
29+
}
30+
31+
vector<vector<int> > threeSum(vector<int> num, int target) {
32+
33+
vector< vector<int> > result;
34+
35+
sort(num.begin(), num.end());
36+
37+
int n = num.size();
38+
39+
for (int i=0; i<n-2; i++) {
40+
//skip the duplication
41+
if (i>0 && num[i-1]==num[i]) continue;
42+
int a = num[i];
43+
int low = i+1;
44+
int high = n-1;
45+
while ( low < high ) {
46+
int b = num[low];
47+
int c = num[high];
48+
if (a+b+c == target) {
49+
//got the soultion
50+
vector<int> v;
51+
v.push_back(a);
52+
v.push_back(b);
53+
v.push_back(c);
54+
result.push_back(v);
55+
// Continue search for all triplet combinations summing to zero.
56+
//skip the duplication
57+
while(low<n && num[low]==num[low+1]) low++;
58+
while(high>0 && num[high]==num[high-1]) high--;
59+
low++;
60+
high--;
61+
} else if (a+b+c > target) {
62+
//skip the duplication
63+
while(high>0 && num[high]==num[high-1]) high--;
64+
high--;
65+
} else{
66+
//skip the duplication
67+
while(low<n && num[low]==num[low+1]) low++;
68+
low++;
69+
}
70+
}
71+
}
72+
return result;
73+
}
74+
75+
76+
int printMatrix(vector< vector<int> > &vv)
77+
{
78+
for(int i=0; i<vv.size(); i++) {
79+
cout << "[";
80+
for(int j=0; j<vv[i].size(); j++) {
81+
cout << " " << vv[i][j];
82+
}
83+
cout << "]" << endl;;
84+
}
85+
}
86+
87+
88+
int main()
89+
{
90+
int a[] = {1,0,-1,0,-2,2};
91+
vector<int> n(a, a+6);
92+
int t = 0;
93+
vector< vector<int> > v = fourSum(n, t);
94+
printMatrix(v);
95+
96+
n.clear();
97+
int b[] = {-1,-5,-5,-3,2,5,0,4};
98+
n.insert(n.begin(), b, b+8);
99+
t = -7;
100+
v = fourSum(n, t);
101+
printMatrix(v);
102+
103+
return 0;
104+
}

0 commit comments

Comments
 (0)