-
Notifications
You must be signed in to change notification settings - Fork 0
/
015three_sum.cpp
44 lines (44 loc) · 1.3 KB
/
015three_sum.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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
//corner case: less than three elements in nums;
if(nums.size() < 3){
return result;
}
//sort the numbers;
sort(nums.begin(), nums.end());
//squeeze for right solution recursively;
for(auto i=nums.begin(); i<nums.end()-2; ++i){
auto j=i+1;
if((i>nums.begin()) && (*i == *(i-1))){
//jump off repetitive values;
continue;
}
auto k = nums.end()-1;
while(j < k){
if(*i + *j + *k < 0){
++j;
while((*j == *(j-1)) && (j < k)){
++j;
}
}
else if(*i + *j + *k > 0){
--k;
while((*k == *(k+1)) && (j < k)){
--k;
}
}
else{
result.push_back({*i, *j, *k});
++j;
--k;
while((*j == *(j-1)) && (*k == *(k+1)) && (j < k)){
++j;
}
}
}
}
return result;
}
};