-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path3sum.cpp
More file actions
34 lines (30 loc) · 959 Bytes
/
Copy path3sum.cpp
File metadata and controls
34 lines (30 loc) · 959 Bytes
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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
vector<vector<int>>res;
std::sort(nums.begin(),nums.end());
for (int i=0;i<nums.size();i++)
{
int target= -nums[i];
int front=i+1;
int end=nums.size()-1;
while(front<end)
{
int sum=nums[front]+nums[end];
if (sum<target)
front++;
else if (sum>target)
end--;
else{
vector<int>temp={nums[i],nums[front],nums[end]};
res.push_back(temp);
while(front<end && nums[front]==temp[1]) front++;
while(front<end && nums[end]==temp[2]) end--;
}
}
while(i+1<nums.size() && nums[i]==nums[i+1]) i++;
}
return res;
}
};