-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy path4Sum.cpp
42 lines (41 loc) · 1.37 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
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
vector<vector<int>> resultVec;
unordered_set<string> setVal;
int n = nums.size();
for(int i=0; i < n-3; i++)
{
for(int j=i+1; j < n-2; j++)
{
int left = j+1;
int right= n-1;
while(left<right)
{
int sum = nums[i]+nums[j]+nums[left]+nums[right];
if(sum > target)
{
right--;
} else if(sum < target)
{
left++;
} else {
vector<int> tempVec{nums[i], nums[j], nums[left], nums[right]};
string hashVal = to_string(nums[i]) + to_string(nums[j]) + to_string(nums[left]) + to_string(nums[right]);
if(setVal.find(hashVal)==setVal.end())
{
resultVec.push_back(tempVec);
setVal.insert(hashVal);
}
left++;
right--;
}
}
}
}
return resultVec;
}
};