forked from shruti170901/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum.cpp
23 lines (22 loc) · 789 Bytes
/
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
// https://leetcode.com/problems/3sum/
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
for(auto it=nums.begin();it!=nums.end();it++){
//cout<<it-nums.begin()<<" ";
for(auto jt=it+1;jt!=nums.end();jt++){
//cout<<*it<<" "<<*jt<<endl;
if(binary_search(jt+1, nums.end(), -(*it+*jt))){
vector<int> temp {*it, *jt, -(*it+*jt)};
ans.push_back(temp);
}
while(jt!=(--nums.end())&&*jt==*(jt+1)) {jt++;}
}
//cout<<it-nums.begin()<<endl;
while(it!=(--nums.end()) && *it==*(it+1)) it++;
}
return ans;
}
};