-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreeSum.cpp
51 lines (42 loc) · 1.05 KB
/
ThreeSum.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
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using std::vector;
vector<vector<int> > treeSum(vector<int> nums){
std::sort(nums.begin(), nums.end());
vector<vector<int> > res;
for(int i = 0; i < nums.size(); i++){
int target = 0 - nums[i];
int front = i + 1;
int back = nums.size() - 1;
while(front < back){
int sum = nums[front] + nums[back];
if(sum < target) front++;
else if(sum > target) back--;
else{
vector<int> temp(3, -1);
temp[0] = nums[i];
temp[1] = nums[front];
temp[2] = nums[back];
res.push_back(temp);
while(front < back && nums[front] == temp[1]) front++;
while(front < back && nums[back] == temp[2]) back--;
}
}
while(i + 1 < nums.size() && nums[i + 1] == nums[i]) i++;
}
return res;
}
int main(){
vector<int> nums = {-1, 0, 1, 2, -1, -4};
std::cout << "result";
vector<vector<int> > res = treeSum(nums);
for(int j=0; j < res.size(); j++){
for(int i=0; i < 3; i++){
std::cout << res[j][i] << "\t";
}
std::cout << std::endl;
}
return 0;
}