forked from rising-entropy/Leetcode-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3sum.cpp
32 lines (29 loc) · 1.03 KB
/
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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n = nums.size(), zerocount = 0;
vector<vector<int>> ans;
set<int> twice;
unordered_map<int, int> found;
for (int i = 0; i < n; i++) {
if (nums[i] == 0) zerocount++;
else if (found.count(nums[i]) > 0) twice.insert(nums[i]);
found[nums[i]] = i;
}
if (zerocount >= 3) ans.push_back({0, 0, 0});
for (int i : twice) {
if (found.count(-2 * i) > 0) ans.push_back({i, i, -2 * i});
}
for (int i = 0; i < n; i++) {
if (found[nums[i]] != i) continue;
for (int j = i + 1; j < n; j++) {
if (found[nums[j]] != j) continue;
int complement = -(nums[i] + nums[j]);
if (found.count(complement) > 0) {
if (found[complement] > j) ans.push_back({nums[i], nums[j], complement});
}
}
}
return ans;
}
};