-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15best.h
49 lines (45 loc) · 1.38 KB
/
15best.h
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
//
// Created by 17336 on 2021/11/2.
//
#ifndef HOT100_15BEST_H
#define HOT100_15BEST_H
#endif //HOT100_15BEST_H
#include "vector"
#include "algorithm"
#include "unordered_set"
using namespace std;
class Solution {
public:
vector<vector<int>> threeSum(vector<int> &nums) {
vector<vector<int>> res;
if (nums.size() < 3) return res;
//排序
sort(nums.begin(), nums.end());
//now记录三数以nums[i]为左数的和为0的中数集合
unordered_set<int> now;
for (int i = 0; i < nums.size() - 2; ++i) {
//如果三数中左数与上一轮不同,一定不重复,清空now
if(i>0&&nums[i]!=nums[i-1])now.clear();
//双指针遍历
int j = i + 1, k = nums.size() - 1;
while (j < k) {
if (nums[j] + nums[k] > -nums[i]) k--;
else if (nums[j] + nums[k] < -nums[i]) j++;
else {
//已经插入过
if(!res.empty()&&now.count(nums[j])) {
j++;
k--;
continue;
}
//没插入过
res.push_back({nums[i],nums[j],nums[k]});
now.insert(nums[j]);
j++;
k--;
}
}
}
return res;
}
};