-
Notifications
You must be signed in to change notification settings - Fork 2
/
00015-3sum.cpp
56 lines (43 loc) · 1.19 KB
/
00015-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
// 15: 3Sum
// https://leetcode.com/problems/3sum/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// SOLUTION
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> result;
for (int i=0; i<nums.size(); i++) {
if (i>0 && nums[i]==nums[i-1]) continue;
int l = i+1;
int r = nums.size() - 1;
while (l<r) {
int s = nums[i] + nums[l] + nums[r];
if (s>0) r--;
else if (s<0) l++;
else {
result.push_back({nums[i], nums[l], nums[r]});
while (nums[l]==nums[l+1]) l++;
while (nums[r]==nums[r-1]) r--;
l++;
r--;
}
}
}
return result;
}
};
int main() {
Solution o;
// INPUT
vector<int> nums = {-1,0,1,2,-1,-4};
// OUTPUT
auto result = o.threeSum(nums);
cout<<"["; for (auto x : result) {
cout<<"["; for (auto y : x) cout<<y<<" "; cout<<"\b] ";
} cout<<"\b]"<<endl;
return 0;
}