-
Notifications
You must be signed in to change notification settings - Fork 82
/
4SUM.cpp
32 lines (32 loc) · 1 KB
/
4SUM.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>> fourSum(vector<int>& nums, int target) {
int n=nums.size();
sort(nums.begin(),nums.end());
vector<vector<int>> ans;
for(int i=0;i<n;i++){
int t3=target-nums[i];
for(int j=i+1;j<n;j++){
int t2=t3-nums[j];
int k=j+1,l=n-1;
while(k<l){
if(nums[k]+nums[l]>t2){
l--;
}
else if(nums[k]+nums[l]<t2){
k++;
}
else{
vector<int> v={nums[i],nums[j],nums[k],nums[l]};
ans.push_back(v);
while(k<l&&nums[k]==v[2])k++;
while(k<l&&nums[l]==v[3])l--;
}
}
while(j+1<n&&nums[j+1]==nums[j])j++;
}
while(i+1<n&&nums[i+1]==nums[i])i++;
}
return ans;
}
};