-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathleetcode15-3sum.cpp
68 lines (62 loc) · 1.93 KB
/
leetcode15-3sum.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> threeSum(vector<int> &nums)
{
int len = nums.size();
vector<vector<int>> res;
sort(nums.begin(), nums.end()); // 先进行排序(从小到大)
if (nums.empty() || nums.front() > 0 || nums.back() < 0) /* 处理临界情况 */
{
return {};
}
for (int i = 0; i < len - 2; i++)
{
if (nums[i] > 0)
{
break;
}
if (i > 0 && nums[i] == nums[i - 1]) /* 注意此处的nums[i] == nums[i-1], 需要覆盖尽量多的case。当循环没到达尾部,且相邻位置出现相等的数组元素时, 跳过 */
{
continue;
}
int target = 0 - nums[i]; // a+b = -c
int j = i + 1;
int k = len - 1;
while (j < k)
{
if (nums[j] + nums[k] == target)
{
res.push_back({nums[i], nums[j], nums[k]});
while (j < k && nums[j] == nums[j + 1]) /* 注意此处的 j<k 不能漏。当区间左侧相邻位置遇到相等的数时, 跳过 */
{
j++;
}
while (j < k && nums[k] == nums[k - 1]) /* 当区间右侧相邻位置遇到相等的数时, 跳过 */
{
k--;
}
j++;
k--;
}
else if (nums[j] + nums[k] < target)
{
j++;
}
else k--;
}
}
return res;
}
};
// Test
int main()
{
Solution sol;
vector<int> input = {-1, 0, 1, 2, -1, -4}; // {0,0,0}
vector<vector<int>> res = sol.threeSum(input);
return 0;
}