Skip to content

Commit bd9928b

Browse files
RJ Trujillohaoel
RJ Trujillo
authored andcommitted
algorithms: (3/4)Sum(Closest): Improve code readability
This just makes the code easier to follow. Signed-off-by: RJ Trujillo <[email protected]>
1 parent e626292 commit bd9928b

File tree

3 files changed

+70
-68
lines changed

3 files changed

+70
-68
lines changed

algorithms/cpp/3Sum/3Sum.cpp

+36-35
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <vector>
2727
#include <set>
2828
#include <algorithm>
29+
2930
using namespace std;
3031

3132

@@ -47,7 +48,7 @@ using namespace std;
4748
vector<vector<int> > threeSum(vector<int> &num) {
4849

4950
vector< vector<int> > result;
50-
if(num.size()==0 || num.size()==1 || num.size() == 2) return result;
51+
if(num.size() == 0 || num.size() == 1 || num.size() == 2) return result;
5152

5253
//sort the array, this is the key
5354
sort(num.begin(), num.end());
@@ -56,14 +57,14 @@ vector<vector<int> > threeSum(vector<int> &num) {
5657

5758
for (int i=0; i<n-2; i++) {
5859
//skip the duplication
59-
if (i>0 && num[i-1]==num[i]) continue;
60+
if (i > 0 && num[i - 1] == num[i]) continue;
6061
int a = num[i];
61-
int low = i+1;
62-
int high = n-1;
63-
while ( low < high ) {
62+
int low = i + 1;
63+
int high = n - 1;
64+
while (low < high) {
6465
int b = num[low];
6566
int c = num[high];
66-
if (a+b+c == 0) {
67+
if (a + b + c == 0) {
6768
//got the soultion
6869
vector<int> v;
6970
v.push_back(a);
@@ -72,17 +73,17 @@ vector<vector<int> > threeSum(vector<int> &num) {
7273
result.push_back(v);
7374
// Continue search for all triplet combinations summing to zero.
7475
//skip the duplication
75-
while(low<n-1 && num[low]==num[low+1]) low++;
76-
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--;
7778
low++;
7879
high--;
7980
} else if (a+b+c > 0) {
8081
//skip the duplication
81-
while(high>0 && num[high]==num[high-1]) high--;
82+
while(high > 0 && num[high] == num[high - 1]) high--;
8283
high--;
83-
} else{
84+
} else {
8485
//skip the duplication
85-
while(low<n-1 && num[low]==num[low+1]) low++;
86+
while(low < n - 1 && num[low] == num[low + 1]) low++;
8687
low++;
8788
}
8889
}
@@ -98,21 +99,21 @@ int sum(vector<int>& v);
9899
vector<vector<int> > threeSum2(vector<int> &num) {
99100
vector< vector<int> > result;
100101
vector< vector<int> > r = combination(num, 3);
101-
for (int i=0; i<r.size(); i++){
102-
if (isSumZero(r[i])){
102+
for (int i = 0; i < r.size(); i++) {
103+
if (isSumZero(r[i])) {
103104
result.push_back(r[i]);
104105
}
105106
}
106107
return result;
107108
}
108109

109-
bool isSumZero(vector<int>& v){
110-
return sum(v)==0;
110+
bool isSumZero(vector < int>& v) {
111+
return sum(v) == 0;
111112
}
112113

113-
int sum(vector<int>& v){
114-
int s=0;
115-
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++) {
116117
s += v[i];
117118
}
118119
return s;
@@ -123,39 +124,39 @@ vector<vector<int> > combination(vector<int> &v, int k) {
123124
vector<vector<int> > result;
124125
vector<int> d;
125126
int n = v.size();
126-
for (int i=0; i<n; i++){
127-
d.push_back( (i<k) ? 1 : 0 );
127+
for (int i = 0; i < n; i++) {
128+
d.push_back( (i < k) ? 1 : 0 );
128129
}
129130

130131
//1) from the left, find the [1,0] pattern, change it to [0,1]
131132
//2) move all of the 1 before the pattern to the most left side
132133
//3) check all of 1 move to the right
133-
while(1){
134+
while(1) {
134135
vector<int> tmp;
135-
for(int x=0; x<n; x++){
136+
for(int x = 0; x < n; x++) {
136137
if (d[x]) tmp.push_back(v[x]);
137138
}
138139
sort(tmp.begin(), tmp.end());
139140
result.push_back(tmp);
140141
//step 1), find [1,0] pattern
141142
int i;
142143
bool found = false;
143-
int ones =0;
144-
for(i=0; i<n-1; i++){
144+
int ones = 0;
145+
for(i = 0; i < n - 1; i++) {
145146

146-
if (d[i]==1 && d[i+1]==0){
147-
d[i]=0; d[i+1]=1;
147+
if (d[i] == 1 && d[i + 1] == 0) {
148+
d[i] = 0; d[i + 1] = 1;
148149
found = true;
149150
//step 2) move all of right 1 to the most left side
150-
for (int j=0; j<i; j++){
151-
d[j]=( ones > 0 ) ? 1 : 0;
151+
for (int j = 0; j < i; j++) {
152+
d[j] = ( ones > 0 ) ? 1 : 0;
152153
ones--;
153154
}
154155
break;
155156
}
156-
if (d[i]==1) ones++;
157+
if (d[i] == 1) ones++;
157158
}
158-
if (!found){
159+
if (!found) {
159160
break;
160161
}
161162

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

167168
void printMatrix(vector<vector<int> > &matrix)
168169
{
169-
for(int i=0; i<matrix.size(); i++){
170+
for(int i = 0; i < matrix.size(); i++) {
170171
printf("{");
171-
for(int j=0; j< matrix[i].size(); j++) {
172+
for(int j = 0; j < matrix[i].size(); j++) {
172173
printf("%3d ", matrix[i][j]) ;
173174
}
174175
printf("}\n");
@@ -179,9 +180,9 @@ void printMatrix(vector<vector<int> > &matrix)
179180

180181
int main()
181182
{
182-
//int a[] = {-1, 0, 1, 2, -1, 1, -4};
183-
int a[] = {-1, 1, 1, 1, -1, -1, 0,0,0};
184-
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));
185186
vector< vector<int> > result = threeSum(n);
186187
printMatrix(result);
187188
return 0;

algorithms/cpp/3SumClosest/3SumClosest.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <vector>
2222
#include <set>
2323
#include <algorithm>
24+
2425
using namespace std;
2526

2627
#define INT_MAX 2147483647
@@ -53,34 +54,33 @@ int threeSumClosest(vector<int> &num, int target) {
5354

5455
for (int i=0; i<n-2; i++) {
5556
//skip the duplication
56-
if (i>0 && num[i-1]==num[i]) continue;
57+
if (i > 0 && num[i - 1] == num[i]) continue;
5758
int a = num[i];
58-
int low = i+1;
59-
int high = n-1;
59+
int low = i + 1;
60+
int high = n - 1;
6061
//convert the 3sum to 2sum problem
61-
while ( low < high ) {
62+
while (low < high) {
6263
int b = num[low];
6364
int c = num[high];
64-
int sum = a+b+c;
65+
int sum = a + b + c;
6566
if (sum - target == 0) {
6667
//got the final soultion
6768
return target;
6869
} else {
69-
7070
//tracking the minmal distance
71-
if (abs(sum-target) < distance ) {
71+
if (abs(sum - target) < distance ) {
7272
distance = abs(sum - target);
7373
result = sum;
7474
}
7575

76-
if (sum -target> 0) {
76+
if (sum - target > 0) {
7777
//skip the duplication
78-
while(high>0 && num[high]==num[high-1]) high--;
78+
while(high > 0 && num[high] == num[high - 1]) high--;
7979
//move the `high` pointer
8080
high--;
8181
} else {
8282
//skip the duplication
83-
while(low<n && num[low]==num[low+1]) low++;
83+
while(low < n && num[low] == num[low + 1]) low++;
8484
//move the `low` pointer
8585
low++;
8686
}
@@ -96,8 +96,8 @@ int threeSumClosest(vector<int> &num, int target) {
9696

9797
int main()
9898
{
99-
int a[] = {-1, 2, 1, -4};
100-
vector<int> n(a, a+sizeof(a)/sizeof(int));
99+
int a[] = { -1, 2, 1, -4 };
100+
vector<int> n(a, a + sizeof(a)/sizeof(int));
101101
int target = 1;
102102
cout << threeSumClosest(n, target) << endl;
103103
return 0;

algorithms/cpp/4Sum/4Sum.cpp

+22-21
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <iostream>
2626
#include <vector>
2727
#include <algorithm>
28+
2829
using namespace std;
2930

3031
vector<vector<int> > threeSum(vector<int> num, int target);
@@ -36,15 +37,15 @@ vector<vector<int> > threeSum(vector<int> num, int target);
3637

3738
vector<vector<int> > fourSum(vector<int> &num, int target) {
3839
vector< vector<int> > result;
39-
if (num.size()<4) return result;
40+
if (num.size() < 4) return result;
4041
sort( num.begin(), num.end() );
41-
42-
for(int i=0; i<num.size()-3; i++) {
42+
43+
for(int i = 0; i < num.size() - 3; i++) {
4344
//skip the duplication
44-
if (i>0 && num[i-1]==num[i]) continue;
45+
if (i > 0 && num[i - 1] == num[i]) continue;
4546
vector<int> n(num.begin()+i+1, num.end());
4647
vector<vector<int> > ret = threeSum(n, target-num[i]);
47-
for(int j=0; j<ret.size(); j++){
48+
for(int j = 0; j < ret.size(); j++) {
4849
ret[j].insert(ret[j].begin(), num[i]);
4950
result.push_back(ret[j]);
5051
}
@@ -61,16 +62,16 @@ vector<vector<int> > threeSum(vector<int> num, int target) {
6162

6263
int n = num.size();
6364

64-
for (int i=0; i<n-2; i++) {
65+
for (int i = 0; i < n - 2; i++) {
6566
//skip the duplication
66-
if (i>0 && num[i-1]==num[i]) continue;
67+
if (i > 0 && num[i - 1] == num[i]) continue;
6768
int a = num[i];
68-
int low = i+1;
69-
int high = n-1;
70-
while ( low < high ) {
69+
int low = i + 1;
70+
int high = n - 1;
71+
while (low < high) {
7172
int b = num[low];
7273
int c = num[high];
73-
if (a+b+c == target) {
74+
if (a + b + c == target) {
7475
//got the soultion
7576
vector<int> v;
7677
v.push_back(a);
@@ -79,17 +80,17 @@ vector<vector<int> > threeSum(vector<int> num, int target) {
7980
result.push_back(v);
8081
// Continue search for all triplet combinations summing to zero.
8182
//skip the duplication
82-
while(low<n && num[low]==num[low+1]) low++;
83-
while(high>0 && num[high]==num[high-1]) high--;
83+
while(low < n && num[low] == num[low + 1]) low++;
84+
while(high > 0 && num[high] == num[high - 1]) high--;
8485
low++;
8586
high--;
86-
} else if (a+b+c > target) {
87+
} else if (a + b + c > target) {
8788
//skip the duplication
88-
while(high>0 && num[high]==num[high-1]) high--;
89+
while(high > 0 && num[high] == num[high - 1]) high--;
8990
high--;
90-
} else{
91+
} else {
9192
//skip the duplication
92-
while(low<n && num[low]==num[low+1]) low++;
93+
while(low < n && num[low] == num[low + 1]) low++;
9394
low++;
9495
}
9596
}
@@ -100,9 +101,9 @@ vector<vector<int> > threeSum(vector<int> num, int target) {
100101

101102
int printMatrix(vector< vector<int> > &vv)
102103
{
103-
for(int i=0; i<vv.size(); i++) {
104+
for(int i = 0; i < vv.size(); i++) {
104105
cout << "[";
105-
for(int j=0; j<vv[i].size(); j++) {
106+
for(int j = 0; j < vv[i].size(); j++) {
106107
cout << " " << vv[i][j];
107108
}
108109
cout << "]" << endl;;
@@ -112,14 +113,14 @@ int printMatrix(vector< vector<int> > &vv)
112113

113114
int main()
114115
{
115-
int a[] = {1,0,-1,0,-2,2};
116+
int a[] = { 1, 0, -1, 0, -2, 2 };
116117
vector<int> n(a, a+6);
117118
int t = 0;
118119
vector< vector<int> > v = fourSum(n, t);
119120
printMatrix(v);
120121

121122
n.clear();
122-
int b[] = {-1,-5,-5,-3,2,5,0,4};
123+
int b[] = { -1, -5, -5, -3, 2, 5, 0, 4 };
123124
n.insert(n.begin(), b, b+8);
124125
t = -7;
125126
v = fourSum(n, t);

0 commit comments

Comments
 (0)