algorithms: (3/4)Sum(Closest): Improve code readability
This just makes the code easier to follow. Signed-off-by: RJ Trujillo <certifiedblyndguy@gmail.com>
This commit is contained in:
parent
e626292120
commit
bd9928bdb1
@ -26,6 +26,7 @@
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
@ -47,7 +48,7 @@ using namespace std;
|
||||
vector<vector<int> > threeSum(vector<int> &num) {
|
||||
|
||||
vector< vector<int> > result;
|
||||
if(num.size()==0 || num.size()==1 || num.size() == 2) return result;
|
||||
if(num.size() == 0 || num.size() == 1 || num.size() == 2) return result;
|
||||
|
||||
//sort the array, this is the key
|
||||
sort(num.begin(), num.end());
|
||||
@ -56,14 +57,14 @@ vector<vector<int> > threeSum(vector<int> &num) {
|
||||
|
||||
for (int i=0; i<n-2; i++) {
|
||||
//skip the duplication
|
||||
if (i>0 && num[i-1]==num[i]) continue;
|
||||
if (i > 0 && num[i - 1] == num[i]) continue;
|
||||
int a = num[i];
|
||||
int low = i+1;
|
||||
int high = n-1;
|
||||
while ( low < high ) {
|
||||
int low = i + 1;
|
||||
int high = n - 1;
|
||||
while (low < high) {
|
||||
int b = num[low];
|
||||
int c = num[high];
|
||||
if (a+b+c == 0) {
|
||||
if (a + b + c == 0) {
|
||||
//got the soultion
|
||||
vector<int> v;
|
||||
v.push_back(a);
|
||||
@ -72,17 +73,17 @@ vector<vector<int> > threeSum(vector<int> &num) {
|
||||
result.push_back(v);
|
||||
// Continue search for all triplet combinations summing to zero.
|
||||
//skip the duplication
|
||||
while(low<n-1 && num[low]==num[low+1]) low++;
|
||||
while(high>0 && num[high]==num[high-1]) high--;
|
||||
while(low < n - 1 && num[low] == num[low + 1]) low++;
|
||||
while(high > 0 && num[high] == num[high - 1]) high--;
|
||||
low++;
|
||||
high--;
|
||||
} else if (a+b+c > 0) {
|
||||
//skip the duplication
|
||||
while(high>0 && num[high]==num[high-1]) high--;
|
||||
while(high > 0 && num[high] == num[high - 1]) high--;
|
||||
high--;
|
||||
} else{
|
||||
} else {
|
||||
//skip the duplication
|
||||
while(low<n-1 && num[low]==num[low+1]) low++;
|
||||
while(low < n - 1 && num[low] == num[low + 1]) low++;
|
||||
low++;
|
||||
}
|
||||
}
|
||||
@ -98,21 +99,21 @@ int sum(vector<int>& v);
|
||||
vector<vector<int> > threeSum2(vector<int> &num) {
|
||||
vector< vector<int> > result;
|
||||
vector< vector<int> > r = combination(num, 3);
|
||||
for (int i=0; i<r.size(); i++){
|
||||
if (isSumZero(r[i])){
|
||||
for (int i = 0; i < r.size(); i++) {
|
||||
if (isSumZero(r[i])) {
|
||||
result.push_back(r[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool isSumZero(vector<int>& v){
|
||||
return sum(v)==0;
|
||||
bool isSumZero(vector < int>& v) {
|
||||
return sum(v) == 0;
|
||||
}
|
||||
|
||||
int sum(vector<int>& v){
|
||||
int s=0;
|
||||
for(int i=0; i<v.size(); i++){
|
||||
int sum(vector<int>& v) {
|
||||
int s = 0;
|
||||
for(int i = 0; i < v.size(); i++) {
|
||||
s += v[i];
|
||||
}
|
||||
return s;
|
||||
@ -123,16 +124,16 @@ vector<vector<int> > combination(vector<int> &v, int k) {
|
||||
vector<vector<int> > result;
|
||||
vector<int> d;
|
||||
int n = v.size();
|
||||
for (int i=0; i<n; i++){
|
||||
d.push_back( (i<k) ? 1 : 0 );
|
||||
for (int i = 0; i < n; i++) {
|
||||
d.push_back( (i < k) ? 1 : 0 );
|
||||
}
|
||||
|
||||
//1) from the left, find the [1,0] pattern, change it to [0,1]
|
||||
//2) move all of the 1 before the pattern to the most left side
|
||||
//3) check all of 1 move to the right
|
||||
while(1){
|
||||
while(1) {
|
||||
vector<int> tmp;
|
||||
for(int x=0; x<n; x++){
|
||||
for(int x = 0; x < n; x++) {
|
||||
if (d[x]) tmp.push_back(v[x]);
|
||||
}
|
||||
sort(tmp.begin(), tmp.end());
|
||||
@ -140,22 +141,22 @@ vector<vector<int> > combination(vector<int> &v, int k) {
|
||||
//step 1), find [1,0] pattern
|
||||
int i;
|
||||
bool found = false;
|
||||
int ones =0;
|
||||
for(i=0; i<n-1; i++){
|
||||
int ones = 0;
|
||||
for(i = 0; i < n - 1; i++) {
|
||||
|
||||
if (d[i]==1 && d[i+1]==0){
|
||||
d[i]=0; d[i+1]=1;
|
||||
if (d[i] == 1 && d[i + 1] == 0) {
|
||||
d[i] = 0; d[i + 1] = 1;
|
||||
found = true;
|
||||
//step 2) move all of right 1 to the most left side
|
||||
for (int j=0; j<i; j++){
|
||||
d[j]=( ones > 0 ) ? 1 : 0;
|
||||
for (int j = 0; j < i; j++) {
|
||||
d[j] = ( ones > 0 ) ? 1 : 0;
|
||||
ones--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (d[i]==1) ones++;
|
||||
if (d[i] == 1) ones++;
|
||||
}
|
||||
if (!found){
|
||||
if (!found) {
|
||||
break;
|
||||
}
|
||||
|
||||
@ -166,9 +167,9 @@ vector<vector<int> > combination(vector<int> &v, int k) {
|
||||
|
||||
void printMatrix(vector<vector<int> > &matrix)
|
||||
{
|
||||
for(int i=0; i<matrix.size(); i++){
|
||||
for(int i = 0; i < matrix.size(); i++) {
|
||||
printf("{");
|
||||
for(int j=0; j< matrix[i].size(); j++) {
|
||||
for(int j = 0; j < matrix[i].size(); j++) {
|
||||
printf("%3d ", matrix[i][j]) ;
|
||||
}
|
||||
printf("}\n");
|
||||
@ -179,9 +180,9 @@ void printMatrix(vector<vector<int> > &matrix)
|
||||
|
||||
int main()
|
||||
{
|
||||
//int a[] = {-1, 0, 1, 2, -1, 1, -4};
|
||||
int a[] = {-1, 1, 1, 1, -1, -1, 0,0,0};
|
||||
vector<int> n(a, a+sizeof(a)/sizeof(int));
|
||||
//int a[] = { -1, 0, 1, 2, -1, 1, -4 };
|
||||
int a[] = { -1, 1, 1, 1, -1, -1, 0,0,0 };
|
||||
vector<int> n(a, a + sizeof(a)/sizeof(int));
|
||||
vector< vector<int> > result = threeSum(n);
|
||||
printMatrix(result);
|
||||
return 0;
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define INT_MAX 2147483647
|
||||
@ -53,34 +54,33 @@ int threeSumClosest(vector<int> &num, int target) {
|
||||
|
||||
for (int i=0; i<n-2; i++) {
|
||||
//skip the duplication
|
||||
if (i>0 && num[i-1]==num[i]) continue;
|
||||
if (i > 0 && num[i - 1] == num[i]) continue;
|
||||
int a = num[i];
|
||||
int low = i+1;
|
||||
int high = n-1;
|
||||
int low = i + 1;
|
||||
int high = n - 1;
|
||||
//convert the 3sum to 2sum problem
|
||||
while ( low < high ) {
|
||||
while (low < high) {
|
||||
int b = num[low];
|
||||
int c = num[high];
|
||||
int sum = a+b+c;
|
||||
int sum = a + b + c;
|
||||
if (sum - target == 0) {
|
||||
//got the final soultion
|
||||
return target;
|
||||
} else {
|
||||
|
||||
//tracking the minmal distance
|
||||
if (abs(sum-target) < distance ) {
|
||||
if (abs(sum - target) < distance ) {
|
||||
distance = abs(sum - target);
|
||||
result = sum;
|
||||
}
|
||||
|
||||
if (sum -target> 0) {
|
||||
if (sum - target > 0) {
|
||||
//skip the duplication
|
||||
while(high>0 && num[high]==num[high-1]) high--;
|
||||
while(high > 0 && num[high] == num[high - 1]) high--;
|
||||
//move the `high` pointer
|
||||
high--;
|
||||
} else {
|
||||
//skip the duplication
|
||||
while(low<n && num[low]==num[low+1]) low++;
|
||||
while(low < n && num[low] == num[low + 1]) low++;
|
||||
//move the `low` pointer
|
||||
low++;
|
||||
}
|
||||
@ -96,8 +96,8 @@ int threeSumClosest(vector<int> &num, int target) {
|
||||
|
||||
int main()
|
||||
{
|
||||
int a[] = {-1, 2, 1, -4};
|
||||
vector<int> n(a, a+sizeof(a)/sizeof(int));
|
||||
int a[] = { -1, 2, 1, -4 };
|
||||
vector<int> n(a, a + sizeof(a)/sizeof(int));
|
||||
int target = 1;
|
||||
cout << threeSumClosest(n, target) << endl;
|
||||
return 0;
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<vector<int> > threeSum(vector<int> num, int target);
|
||||
@ -36,15 +37,15 @@ vector<vector<int> > threeSum(vector<int> num, int target);
|
||||
|
||||
vector<vector<int> > fourSum(vector<int> &num, int target) {
|
||||
vector< vector<int> > result;
|
||||
if (num.size()<4) return result;
|
||||
if (num.size() < 4) return result;
|
||||
sort( num.begin(), num.end() );
|
||||
|
||||
for(int i=0; i<num.size()-3; i++) {
|
||||
|
||||
for(int i = 0; i < num.size() - 3; i++) {
|
||||
//skip the duplication
|
||||
if (i>0 && num[i-1]==num[i]) continue;
|
||||
if (i > 0 && num[i - 1] == num[i]) continue;
|
||||
vector<int> n(num.begin()+i+1, num.end());
|
||||
vector<vector<int> > ret = threeSum(n, target-num[i]);
|
||||
for(int j=0; j<ret.size(); j++){
|
||||
for(int j = 0; j < ret.size(); j++) {
|
||||
ret[j].insert(ret[j].begin(), num[i]);
|
||||
result.push_back(ret[j]);
|
||||
}
|
||||
@ -61,16 +62,16 @@ vector<vector<int> > threeSum(vector<int> num, int target) {
|
||||
|
||||
int n = num.size();
|
||||
|
||||
for (int i=0; i<n-2; i++) {
|
||||
for (int i = 0; i < n - 2; i++) {
|
||||
//skip the duplication
|
||||
if (i>0 && num[i-1]==num[i]) continue;
|
||||
if (i > 0 && num[i - 1] == num[i]) continue;
|
||||
int a = num[i];
|
||||
int low = i+1;
|
||||
int high = n-1;
|
||||
while ( low < high ) {
|
||||
int low = i + 1;
|
||||
int high = n - 1;
|
||||
while (low < high) {
|
||||
int b = num[low];
|
||||
int c = num[high];
|
||||
if (a+b+c == target) {
|
||||
if (a + b + c == target) {
|
||||
//got the soultion
|
||||
vector<int> v;
|
||||
v.push_back(a);
|
||||
@ -79,17 +80,17 @@ vector<vector<int> > threeSum(vector<int> num, int target) {
|
||||
result.push_back(v);
|
||||
// Continue search for all triplet combinations summing to zero.
|
||||
//skip the duplication
|
||||
while(low<n && num[low]==num[low+1]) low++;
|
||||
while(high>0 && num[high]==num[high-1]) high--;
|
||||
while(low < n && num[low] == num[low + 1]) low++;
|
||||
while(high > 0 && num[high] == num[high - 1]) high--;
|
||||
low++;
|
||||
high--;
|
||||
} else if (a+b+c > target) {
|
||||
} else if (a + b + c > target) {
|
||||
//skip the duplication
|
||||
while(high>0 && num[high]==num[high-1]) high--;
|
||||
while(high > 0 && num[high] == num[high - 1]) high--;
|
||||
high--;
|
||||
} else{
|
||||
} else {
|
||||
//skip the duplication
|
||||
while(low<n && num[low]==num[low+1]) low++;
|
||||
while(low < n && num[low] == num[low + 1]) low++;
|
||||
low++;
|
||||
}
|
||||
}
|
||||
@ -100,9 +101,9 @@ vector<vector<int> > threeSum(vector<int> num, int target) {
|
||||
|
||||
int printMatrix(vector< vector<int> > &vv)
|
||||
{
|
||||
for(int i=0; i<vv.size(); i++) {
|
||||
for(int i = 0; i < vv.size(); i++) {
|
||||
cout << "[";
|
||||
for(int j=0; j<vv[i].size(); j++) {
|
||||
for(int j = 0; j < vv[i].size(); j++) {
|
||||
cout << " " << vv[i][j];
|
||||
}
|
||||
cout << "]" << endl;;
|
||||
@ -112,14 +113,14 @@ int printMatrix(vector< vector<int> > &vv)
|
||||
|
||||
int main()
|
||||
{
|
||||
int a[] = {1,0,-1,0,-2,2};
|
||||
int a[] = { 1, 0, -1, 0, -2, 2 };
|
||||
vector<int> n(a, a+6);
|
||||
int t = 0;
|
||||
vector< vector<int> > v = fourSum(n, t);
|
||||
printMatrix(v);
|
||||
|
||||
n.clear();
|
||||
int b[] = {-1,-5,-5,-3,2,5,0,4};
|
||||
int b[] = { -1, -5, -5, -3, 2, 5, 0, 4 };
|
||||
n.insert(n.begin(), b, b+8);
|
||||
t = -7;
|
||||
v = fourSum(n, t);
|
||||
|
Loading…
x
Reference in New Issue
Block a user