-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_3Sum.cpp
80 lines (68 loc) · 1.79 KB
/
15_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
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <unordered_map>
using namespace std;
static vector<vector<int>>
threeSum(vector<int>& nums) {
int size = nums.size();
vector<vector<int>> res;
if (size == 0)
return res;
sort(nums.begin(), nums.end());
for (int i = 0; i < size - 2; ++i) {
if (i > 0 && nums[i-1] == nums[i]) {
continue;
}
// unordered_map<int, int> numMap;
// for (int j = i + 1; j != size; ++j) {
// // 这里的处理并不是把需要找的值 -nums[i]-nums[j]直接去寻找,而是记录在容器中,继续向后遍历, 在后续的遍历中判断 当前遍历到的值是否是之前需要的, 这样做减少了一层遍历
// if (numMap.end() != numMap.find(nums[j]) && 1 == numMap[nums[j]]) {
// res.push_back({nums[i], -nums[i] - nums[j], nums[j]});
// numMap[nums[j]] = 0;
// }
// else {
// numMap.insert({-nums[i] - nums[j], 1});
// }
// }
int l = i+1;
int r = size - 1;
while (l < r) {
int s = nums[i] + nums[l] + nums[r];
if (s > 0) {
r--;
}
else if (s < 0) {
l++;
}
else {
res.push_back({nums[i], nums[l], nums[r]});
while (l < r && nums[l] == nums[l+1]) {
l++;
}
while (l < r && nums[r] == nums[r-1]) {
r--;
}
l++;
r--;
}
}
}
return res;
}
int
main(int argc, char const *argv[])
{
vector<int> array = {-1, 0, 1, 2, -1, -4};
vector<vector<int>> res = threeSum(array);
for (auto i : res) {
cout << "[";
for (auto j : i) {
cout << j << " ";
}
cout << "]";
}
return 0;
}