-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.3-sum.cpp
67 lines (64 loc) · 2.14 KB
/
15.3-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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* @lc app=leetcode id=15 lang=cpp
*
* [15] 3Sum
*/
class Solution {
public:
/* Hash table solution
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end()); // ?
for (int i = 0; i < nums.size(); i++)
{
// i points to c
if (i < nums.size() - 1 && nums[i] == nums[i + 1]) continue;
unordered_set<int> num_map;
for (int j = 0;j < i;j++)
{
if (num_map.count(-nums[i] - nums[j]))
{
result.push_back({-nums[i] - nums[j], nums[j], nums[i]});
while (nums[j+1] == nums[j] && j < i - 1) j++;
}
num_map.insert(nums[j]);
}
}
return result;
}
*/
// two pointer solution
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
int left,right;
int lvalue,rvalue;
int size = nums.size();
for (int i = 2; i < size; i++)
{
if(i < nums.size() - 1 && nums[i] == nums[i+1]) continue;
left = 0;
right = i - 1;
while(right > left){
lvalue = nums[left];
rvalue = nums[right];
if (lvalue + rvalue + nums[i] == 0){
result.push_back({lvalue, rvalue, nums[i]});
while(right > left && nums[left] == lvalue) left++;
while(right > left && nums[right] == rvalue) right--;
}
else if (nums[left] + nums[right] > -nums[i])
{
// cout<<"test7"<<endl;
while(right > left && nums[right] == rvalue) right--;
}
else
{
// cout<<"test8"<<endl;
while(right > left && nums[left] == lvalue) left++;
}
}
}
return result;
}
};