Repository navigation
Expand file tree
/
Copy path4sum.cpp
More file actions
49 lines (48 loc) · 1.58 KB
/
Copy path4sum.cpp
File metadata and controls
49 lines (48 loc) · 1.58 KB
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
class Solution {
public:
int first, second;
vector<vector<int>> res;
void twoSum(vector<int>& nums, int start, int end, int target) {
int n = end-start+1;
if(n < 2) return;
int left = start, right = end;
while(left < right) {
int curSum = nums[left] + nums[right];
if(curSum == target) {
vector<int> newResult;
newResult.push_back(first);
newResult.push_back(second);
newResult.push_back(nums[left]);
newResult.push_back(nums[right]);
res.push_back(newResult);
right--;
while(left < right && nums[right] == nums[right+1])
right--;
} else if(curSum < target) {
left++;
} else if(curSum > target) {
right--;
}
}
}
void threeSum(vector<int>& nums, int start, int end, int target) {
int n = end-start+1;
if(n < 3) return;
for(int i = start; i <= end; i++) {
if(i > start && nums[i] == nums[i-1]) continue;
second = nums[i];
twoSum(nums, i+1, end, target-second);
}
}
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n = nums.size();
if(n < 4) return res;
sort(nums.begin(), nums.end());
for(int i = 0; i < n-3; i++) {
if(i && nums[i] == nums[i-1]) continue;
first = nums[i];
threeSum(nums, i+1, n-1, target-first);
}
return res;
}
};