-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.cpp
More file actions
29 lines (28 loc) · 1.02 KB
/
3Sum.cpp
File metadata and controls
29 lines (28 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
// Method: two pointers
// Space Complexity: O(1)
// Time complexity: O(n^2)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end()); // time: O(nlogn)
vector<vector<int>> res;
for (int i=0; i<nums.size(); i++) {
if (i > 0 && nums[i] == nums[i-1]) continue; // avoid duplicates
int target = -nums[i]; // the target sum of the numbers of two pointers is -nums[i]
int l = i + 1, r = nums.size() - 1; // two pointers l and r
while (l < r) {
int nl = nums[l], nr = nums[r];
if (nl + nr == target) {
res.push_back({nums[i], nl, nr});
while (nums[l] == nl) l++; // avoid duplicates
while (nums[r] == nr) r--; // avoid duplicates
} else if (nl + nr > target) {
r--;
} else {
l++;
}
}
}
return res;
}
};