-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.cpp
More file actions
34 lines (33 loc) · 1.02 KB
/
3Sum.cpp
File metadata and controls
34 lines (33 loc) · 1.02 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
// Source: https://leetcode.com/problems/3sum/
// Author: Miao Zhang
// Date: 2021-01-05
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> res;
int n = nums.size();
for (int i = 0; i < n - 2; i++) {
if (i > 0 && nums[i - 1] == nums[i]) continue;
int target = -1 * nums[i];
int l = i + 1;
int r = n - 1;
while (l < r) {
if (nums[l] + nums[r] == target) {
vector<int> tmp;
tmp.push_back(nums[i]);
tmp.push_back(nums[l]);
tmp.push_back(nums[r]);
res.push_back(tmp);
l++;
while (l < r && nums[l] == nums[l - 1]) l++;
} else if (nums[l] + nums[r] < target) {
l++;
} else {
r--;
}
}
}
return res;
}
};