-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeSum.cpp
More file actions
49 lines (48 loc) · 1.87 KB
/
ThreeSum.cpp
File metadata and controls
49 lines (48 loc) · 1.87 KB
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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> ret;
int len = nums.size();
if (len < 3) return ret;
sort(nums.begin(), nums.end());
for (int first = 0; first < len; first++) {
if (nums[first] > 0) break;
if (first > 0 && nums[first] == nums[first - 1]) continue;
int target = -nums[first];
for (int second = first + 1, third = len - 1; second < len; second++) {
if (second > first + 1 && nums[second] == nums[second - 1]) continue;
while (second < third && nums[second] + nums[third] > target) third--;
if (second == third) break;
if (nums[second] + nums[third] == target) ret.push_back({nums[first], nums[second], nums[third]});
}
}
return ret;
}
};
class Solution2 {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> ret;
int len = nums.size();
if (len < 3) return ret;
sort(nums.begin(), nums.end());
for (int first = 0; first < len; first++) {
if (nums[first] > 0) break;
if (first > 0 && nums[first] == nums[first - 1]) continue;
int second = first + 1, third = len - 1;
while (second < third) {
int sum = nums[first] + nums[second] + nums[third];
if (sum == 0) {
ret.push_back({nums[first], nums[second], nums[third]});
while (second < third && nums[second] == nums[second + 1]) second++;
while (second < third && nums[third] == nums[third - 1]) third--;
second++;
third--;
}
else if (sum > 0) third--;
else second++;
}
}
return ret;
}
};