-
Notifications
You must be signed in to change notification settings - Fork 0
/
3Sum.cc
28 lines (25 loc) · 840 Bytes
/
3Sum.cc
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
#include "bits/stdc++.h"
using namespace std;
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
for(int a=0; a<nums.size()-2; ++a) {
while(a!=0 && a<nums.size()-2 && nums[a]==nums[a-1]) ++a;
int l = a+1, r = nums.size()-1;
while(l < r) {
if(nums[a] + nums[l] + nums[r] > 0) --r;
else if(nums[a] + nums[l] + nums[r] <0) ++l;
else {
vector<int> temp = {nums[a], nums[l], nums[r]};
ans.emplace_back(temp);
while(l < nums.size() && nums[l] == temp[1] && l < r) ++l;
while(r > 0 && nums[r] == temp[2] && l < r) --r;
}
}
}
return ans;
}
int main() {
vector<int> temp = {0, 0 ,0, 0};
cout << threeSum(temp)[0][1];
}