-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC - 15. 3Sum
65 lines (63 loc) · 1.86 KB
/
LC - 15. 3Sum
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
// Approach 1 (using set)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
if(!nums.size() || nums.size() < 3) return {};
set<vector<int>> temp;
vector<vector<int>> ans;
sort(nums.begin(), nums.end());
for(int i = 0; i <= nums.size()-3; i++)
{
int target = -nums[i];
int lo = i+1;
int hi = nums.size() -1;
while(lo < hi)
{
if(nums[lo] + nums[hi] == target)
{
temp.insert({nums[i], nums[lo], nums[hi]});
lo++;
hi--;
}
else if(nums[lo] + nums[hi] < target) lo++;
else hi--;
}
}
for(auto v : temp) ans.push_back(v);
return ans;
}
};
// approach 2
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
if(!nums.size() || nums.size() < 3) return {};
vector<vector<int>> ans;
sort(nums.begin(), nums.end());
for(int i = 0; i <= nums.size()-3; i++)
{
int target = -nums[i];
int lo = i+1;
int hi = nums.size() -1;
while(lo < hi)
{
if(nums[lo] + nums[hi] == target)
{
vector<int> v(3,0);
v[0] = nums[i];
v[1] = nums[lo];
v[2] = nums[hi];
ans.push_back(v);
while(lo < hi && nums[lo] == v[1] ) lo++;
while(hi > lo && nums[hi] == v[2] ) hi--;
}
else if(nums[lo] + nums[hi] < target) lo++;
else hi--;
}
while(i+1 < nums.size() && nums[i+1] == nums[i] ) i++;
}
return ans;
}
};