Skip to content

Commit a9393ef

Browse files
authored
Merge pull request #1 from haoel/master
Sync code
2 parents 540738b + b14af19 commit a9393ef

File tree

579 files changed

+27916
-1161
lines changed

Some content is hidden

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

579 files changed

+27916
-1161
lines changed

.gitconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[alias]
2+
lg0 = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --
3+
lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
4+
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
5+
lg = !"git lg0"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
algorithms-java/out
3+
*.class

README.md

Lines changed: 432 additions & 157 deletions
Large diffs are not rendered by default.

src/3Sum/3Sum.cpp renamed to algorithms/cpp/3Sum/3Sum.cpp

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,45 @@
2626
#include <vector>
2727
#include <set>
2828
#include <algorithm>
29+
2930
using namespace std;
3031

3132

32-
//solution: http://en.wikipedia.org/wiki/3SUM
33+
/*
34+
* Similar like "Two Number" problem, we can have the simlar solution.
35+
*
36+
* Suppose the input array is S[0..n-1], 3SUM can be solved in O(n^2) time on average by
37+
* inserting each number S[i] into a hash table, and then for each index i and j,
38+
* checking whether the hash table contains the integer - (s[i]+s[j])
39+
*
40+
* Alternatively, the algorithm below first sorts the input array and then tests all
41+
* possible pairs in a careful order that avoids the need to binary search for the pairs
42+
* in the sorted list, achieving worst-case O(n^n)
43+
*
44+
* Solution: Quadratic algorithm
45+
* http://en.wikipedia.org/wiki/3SUM
46+
*
47+
*/
3348
vector<vector<int> > threeSum(vector<int> &num) {
3449

3550
vector< vector<int> > result;
51+
if(num.size() == 0 || num.size() == 1 || num.size() == 2) return result;
3652

53+
//sort the array, this is the key
3754
sort(num.begin(), num.end());
3855

3956
int n = num.size();
4057

4158
for (int i=0; i<n-2; i++) {
4259
//skip the duplication
43-
if (i>0 && num[i-1]==num[i]) continue;
60+
if (i > 0 && num[i - 1] == num[i]) continue;
4461
int a = num[i];
45-
int low = i+1;
46-
int high = n-1;
47-
while ( low < high ) {
62+
int low = i + 1;
63+
int high = n - 1;
64+
while (low < high) {
4865
int b = num[low];
4966
int c = num[high];
50-
if (a+b+c == 0) {
67+
if (a + b + c == 0) {
5168
//got the soultion
5269
vector<int> v;
5370
v.push_back(a);
@@ -56,17 +73,17 @@ vector<vector<int> > threeSum(vector<int> &num) {
5673
result.push_back(v);
5774
// Continue search for all triplet combinations summing to zero.
5875
//skip the duplication
59-
while(low<n && num[low]==num[low+1]) low++;
60-
while(high>0 && num[high]==num[high-1]) high--;
76+
while(low < n - 1 && num[low] == num[low + 1]) low++;
77+
while(high > 0 && num[high] == num[high - 1]) high--;
6178
low++;
6279
high--;
6380
} else if (a+b+c > 0) {
6481
//skip the duplication
65-
while(high>0 && num[high]==num[high-1]) high--;
82+
while(high > 0 && num[high] == num[high - 1]) high--;
6683
high--;
67-
} else{
84+
} else {
6885
//skip the duplication
69-
while(low<n && num[low]==num[low+1]) low++;
86+
while(low < n - 1 && num[low] == num[low + 1]) low++;
7087
low++;
7188
}
7289
}
@@ -82,21 +99,21 @@ int sum(vector<int>& v);
8299
vector<vector<int> > threeSum2(vector<int> &num) {
83100
vector< vector<int> > result;
84101
vector< vector<int> > r = combination(num, 3);
85-
for (int i=0; i<r.size(); i++){
86-
if (isSumZero(r[i])){
102+
for (int i = 0; i < r.size(); i++) {
103+
if (isSumZero(r[i])) {
87104
result.push_back(r[i]);
88105
}
89106
}
90107
return result;
91108
}
92109

93-
bool isSumZero(vector<int>& v){
94-
return sum(v)==0;
110+
bool isSumZero(vector < int>& v) {
111+
return sum(v) == 0;
95112
}
96113

97-
int sum(vector<int>& v){
98-
int s=0;
99-
for(int i=0; i<v.size(); i++){
114+
int sum(vector<int>& v) {
115+
int s = 0;
116+
for(int i = 0; i < v.size(); i++) {
100117
s += v[i];
101118
}
102119
return s;
@@ -107,39 +124,39 @@ vector<vector<int> > combination(vector<int> &v, int k) {
107124
vector<vector<int> > result;
108125
vector<int> d;
109126
int n = v.size();
110-
for (int i=0; i<n; i++){
111-
d.push_back( (i<k) ? 1 : 0 );
127+
for (int i = 0; i < n; i++) {
128+
d.push_back( (i < k) ? 1 : 0 );
112129
}
113130

114131
//1) from the left, find the [1,0] pattern, change it to [0,1]
115132
//2) move all of the 1 before the pattern to the most left side
116133
//3) check all of 1 move to the right
117-
while(1){
134+
while(1) {
118135
vector<int> tmp;
119-
for(int x=0; x<n; x++){
136+
for(int x = 0; x < n; x++) {
120137
if (d[x]) tmp.push_back(v[x]);
121138
}
122139
sort(tmp.begin(), tmp.end());
123140
result.push_back(tmp);
124141
//step 1), find [1,0] pattern
125142
int i;
126143
bool found = false;
127-
int ones =0;
128-
for(i=0; i<n-1; i++){
144+
int ones = 0;
145+
for(i = 0; i < n - 1; i++) {
129146

130-
if (d[i]==1 && d[i+1]==0){
131-
d[i]=0; d[i+1]=1;
147+
if (d[i] == 1 && d[i + 1] == 0) {
148+
d[i] = 0; d[i + 1] = 1;
132149
found = true;
133150
//step 2) move all of right 1 to the most left side
134-
for (int j=0; j<i; j++){
135-
d[j]=( ones > 0 ) ? 1 : 0;
151+
for (int j = 0; j < i; j++) {
152+
d[j] = ( ones > 0 ) ? 1 : 0;
136153
ones--;
137154
}
138155
break;
139156
}
140-
if (d[i]==1) ones++;
157+
if (d[i] == 1) ones++;
141158
}
142-
if (!found){
159+
if (!found) {
143160
break;
144161
}
145162

@@ -150,9 +167,9 @@ vector<vector<int> > combination(vector<int> &v, int k) {
150167

151168
void printMatrix(vector<vector<int> > &matrix)
152169
{
153-
for(int i=0; i<matrix.size(); i++){
170+
for(int i = 0; i < matrix.size(); i++) {
154171
printf("{");
155-
for(int j=0; j< matrix[i].size(); j++) {
172+
for(int j = 0; j < matrix[i].size(); j++) {
156173
printf("%3d ", matrix[i][j]) ;
157174
}
158175
printf("}\n");
@@ -163,9 +180,9 @@ void printMatrix(vector<vector<int> > &matrix)
163180

164181
int main()
165182
{
166-
//int a[] = {-1, 0, 1, 2, -1, 1, -4};
167-
int a[] = {-1, 1, 1, 1, -1, -1, 0,0,0};
168-
vector<int> n(a, a+sizeof(a)/sizeof(int));
183+
//int a[] = { -1, 0, 1, 2, -1, 1, -4 };
184+
int a[] = { -1, 1, 1, 1, -1, -1, 0,0,0 };
185+
vector<int> n(a, a + sizeof(a)/sizeof(int));
169186
vector< vector<int> > result = threeSum(n);
170187
printMatrix(result);
171188
return 0;
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/3sum-closest/
2+
// Author : Hao Chen
3+
// Date : 2014-07-03
4+
5+
/**********************************************************************************
6+
*
7+
* Given an array S of n integers, find three integers in S such that the sum is
8+
* closest to a given number, target. Return the sum of the three integers.
9+
* You may assume that each input would have exactly one solution.
10+
*
11+
* For example, given array S = {-1 2 1 -4}, and target = 1.
12+
*
13+
* The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
14+
*
15+
*
16+
**********************************************************************************/
17+
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
#include <iostream>
21+
#include <vector>
22+
#include <set>
23+
#include <algorithm>
24+
25+
using namespace std;
26+
27+
#define INT_MAX 2147483647
28+
//solution: http://en.wikipedia.org/wiki/3SUM
29+
//the idea as blow:
30+
// 1) sort the array.
31+
// 2) take the element one by one, calculate the two numbers in reset array.
32+
//
33+
//notes: be careful the duplication number.
34+
//
35+
// for example:
36+
// [-4,-1,-1,1,2] target=1
37+
//
38+
// take -4, can cacluate the "two number problem" of the reset array [-1,-1,1,2] while target=5
39+
// [(-4),-1,-1,1,2] target=5 distance=4
40+
// ^ ^
41+
// because the -1+2 = 1 which < 5, then move the `low` pointer(skip the duplication)
42+
// [(-4),-1,-1,1,2] target=5 distance=2
43+
// ^ ^
44+
// take -1(skip the duplication), can cacluate the "two number problem" of the reset array [1,2] while target=2
45+
// [-4,-1,(-1),1,2] target=2 distance=1
46+
// ^ ^
47+
int threeSumClosest(vector<int> &num, int target) {
48+
//sort the array
49+
sort(num.begin(), num.end());
50+
51+
int n = num.size();
52+
int distance = INT_MAX;
53+
int result;
54+
55+
for (int i=0; i<n-2; i++) {
56+
//skip the duplication
57+
if (i > 0 && num[i - 1] == num[i]) continue;
58+
int a = num[i];
59+
int low = i + 1;
60+
int high = n - 1;
61+
//convert the 3sum to 2sum problem
62+
while (low < high) {
63+
int b = num[low];
64+
int c = num[high];
65+
int sum = a + b + c;
66+
if (sum - target == 0) {
67+
//got the final soultion
68+
return target;
69+
} else {
70+
//tracking the minmal distance
71+
if (abs(sum - target) < distance ) {
72+
distance = abs(sum - target);
73+
result = sum;
74+
}
75+
76+
if (sum - target > 0) {
77+
//skip the duplication
78+
while(high > 0 && num[high] == num[high - 1]) high--;
79+
//move the `high` pointer
80+
high--;
81+
} else {
82+
//skip the duplication
83+
while(low < n && num[low] == num[low + 1]) low++;
84+
//move the `low` pointer
85+
low++;
86+
}
87+
}
88+
}
89+
}
90+
91+
return result;
92+
}
93+
94+
95+
96+
97+
int main()
98+
{
99+
int a[] = { -1, 2, 1, -4 };
100+
vector<int> n(a, a + sizeof(a)/sizeof(int));
101+
int target = 1;
102+
cout << threeSumClosest(n, target) << endl;
103+
return 0;
104+
}

0 commit comments

Comments
 (0)