-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.3-sum.cpp
36 lines (33 loc) · 950 Bytes
/
15.3-sum.cpp
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
/*
* @lc app=leetcode id=15 lang=cpp
*
* [15] 3Sum
*/
// @lc code=start
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
// -4 -1 -1 0 1 2
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
for (int i = 0; i < nums.size() - 2; i++) {
if (i > 0) {
if (nums[i] == nums[i - 1]) continue;
}
int j = i + 1, k = nums.size() - 1;
while (j < k) {
if (nums[i] + nums[j] + nums[k] < 0) j++;
else if (nums[i] + nums[j] + nums[k] > 0) k--;
else {
ans.push_back({nums[i], nums[j], nums[k]});
int j_value = nums[j], k_value = nums[k];
do {
j++;
} while (nums[j] == j_value && j < k);
}
}
}
return ans;
}
};
// @lc code=end