-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum.cpp
30 lines (27 loc) · 1007 Bytes
/
3Sum.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
//treeSum use the different solution od 2 sum because 2 sum uses hash table to search in O(n)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
if(nums.empty()) return res;
sort(nums.begin(), nums.end());
nums.remove(unique(nums.begin, nums.end()), nums.end());//have duplicate num in this case
int N = nums.size()
for(int first = 0; first < N-2; first++){ // for the fixed first num
int second = first +1;
int third = N - 1;
while(second < third){
int sum = nums[first] + nums[second] + nums[third]
if((sum < 0)
second++;
else if(sum > 0)
third--;
else if(sum == 0){
res.push_back({first, second, third});
second++;
}
}
}
return sum;
}
};