-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution15.cpp
More file actions
38 lines (35 loc) · 1.08 KB
/
Solution15.cpp
File metadata and controls
38 lines (35 loc) · 1.08 KB
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
//
// Solution15.cpp
// Algorithm
//
// Created by Pancf on 2020/10/11.
// Copyright © 2020 Pancf. All rights reserved.
//
#include "Solution15.hpp"
#include <algorithm>
std::vector<std::vector<int>> Solution15::threeSum(std::vector<int>& nums)
{
std::vector<std::vector<int>> res;
if (nums.size() < 3) return res;
std::sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size() - 2 && nums[i] <= 0; ++i) {
int sum = 0 - nums[i];
int low = i + 1;
int high = nums.size() - 1;
while (low < high) {
if (sum == nums[low] + nums[high]) {
res.push_back({nums[i], nums[low], nums[high]});
while (low < high && nums[low] == nums[low + 1]) ++low;
while (low < high && nums[high] == nums[high - 1]) --high;
++low;
--high;
} else if (sum > nums[low] + nums[high]) {
++low;
} else {
--high;
}
}
while (nums[i] == nums[i + 1] && i < nums.size() - 2) ++i;
}
return res;
}