forked from weiliu89/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4Sum.cpp
129 lines (112 loc) · 3.48 KB
/
4Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Source : https://oj.leetcode.com/problems/4sum/
// Author : Hao Chen
// Date : 2014-07-03
/**********************************************************************************
*
* Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?
* Find all unique quadruplets in the array which gives the sum of target.
*
* Note:
*
* Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
* The solution set must not contain duplicate quadruplets.
*
* For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
*
* A solution set is:
* (-1, 0, 0, 1)
* (-2, -1, 1, 2)
* (-2, 0, 0, 2)
*
*
**********************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int> > threeSum(vector<int> num, int target);
/*
* 1) Sort the array,
* 2) traverse the array, and solve the problem by using "3Sum" soultion.
*/
vector<vector<int> > fourSum(vector<int> &num, int target) {
vector< vector<int> > result;
if (num.size()<4) return result;
sort( num.begin(), num.end() );
for(int i=0; i<num.size()-3; i++) {
//skip the duplication
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++){
ret[j].insert(ret[j].begin(), num[i]);
result.push_back(ret[j]);
}
}
return result;
}
vector<vector<int> > threeSum(vector<int> num, int target) {
vector< vector<int> > result;
//sort the array (if the qrray is sorted already, it won't waste any time)
sort(num.begin(), num.end());
int n = num.size();
for (int i=0; i<n-2; i++) {
//skip the duplication
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 b = num[low];
int c = num[high];
if (a+b+c == target) {
//got the soultion
vector<int> v;
v.push_back(a);
v.push_back(b);
v.push_back(c);
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--;
low++;
high--;
} else if (a+b+c > target) {
//skip the duplication
while(high>0 && num[high]==num[high-1]) high--;
high--;
} else{
//skip the duplication
while(low<n && num[low]==num[low+1]) low++;
low++;
}
}
}
return result;
}
int printMatrix(vector< vector<int> > &vv)
{
for(int i=0; i<vv.size(); i++) {
cout << "[";
for(int j=0; j<vv[i].size(); j++) {
cout << " " << vv[i][j];
}
cout << "]" << endl;;
}
}
int main()
{
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};
n.insert(n.begin(), b, b+8);
t = -7;
v = fourSum(n, t);
printMatrix(v);
return 0;
}