-
Notifications
You must be signed in to change notification settings - Fork 0
/
058. 四数之和.cpp
50 lines (50 loc) · 1.67 KB
/
058. 四数之和.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
class Solution {
public:
/*
* @param numbers: Give an array
* @param target: An integer
* @return: Find all unique quadruplets in the array which gives the sum of zero
*/
vector<vector<int>> fourSum(vector<int> numbers, int target) {
// write your code here
vector<vector<int>> res;
sort(numbers.begin(), numbers.end());
int size = numbers.size();
for(int i=0; i<size; i++)
{
for(int j=i+1; j<size; j++)
{
int k=j+1, h=size-1;
while(k<h)
{
int sum = numbers[i] + numbers[j] + numbers[k] + numbers[h];
vector<int> temp;
temp.resize(0);
if(sum == target)
{
temp.push_back(numbers[i]);
temp.push_back(numbers[j]);
temp.push_back(numbers[k]);
temp.push_back(numbers[h]);
bool ok = true;
for(int index=0; index<res.size(); index++)
{
if(res[index] == temp)
{
ok = false;
break;
}
}
if(ok)
res.push_back(temp);
}
if(sum <= target)
k++;
else
h--;
}
}
}
return res;
}
};