forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
93 lines (73 loc) · 2.43 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/// Source : https://leetcode.com/problems/4sum/
/// Author : liuyubobobo
/// Time : 2016-12-06
#include <iostream>
#include <vector>
#include <cassert>
#include <stdexcept>
using namespace std;
/// Two pointers
/// Sort the array first.
/// For every different number a and b, try to find a pair (c, d), which a + b + c + d == 0
/// Using this way, we don't need to see whether the triplet is a repeated one
///
/// Time Complexity: O(nlogn) + O(n^3)
/// Space Complexity: O(1)
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n = nums.size();
vector<vector<int>> res;
if(n < 4)
return res;
sort(nums.begin(), nums.end());
for(int i = 0 ; i <= n - 4 ; i = nextNumberIndex(nums, i))
for (int j = i + 1; j <= n - 3; j = nextNumberIndex(nums, j)) {
int t = target - nums[i] - nums[j];
if(nums[j+1] + nums[j+2] > t || nums[n-1] + nums[n-2] < t)
continue;
int p1 = j + 1;
int p2 = nums.size() - 1;
if (p1 >= p2)
break;
while (p1 < p2) {
if (nums[p1] + nums[p2] == t) {
res.push_back({nums[i], nums[j], nums[p1], nums[p2]});
p1 = nextNumberIndex(nums, p1);
p2 = preNumberIndex(nums, p2);
}
else if (nums[p1] + nums[p2] < t)
p1 = nextNumberIndex(nums, p1);
else // nums[p1] + nums[p2] > t
p2 = preNumberIndex(nums, p2);
}
}
return res;
}
private:
int nextNumberIndex( const vector<int> &nums , int index ){
for( int i = index + 1 ; i < nums.size() ; i ++ )
if( nums[i] != nums[index] )
return i;
return nums.size();
}
int preNumberIndex( const vector<int> &nums , int index ){
for( int i = index-1 ; i >= 0 ; i -- )
if( nums[i] != nums[index] )
return i;
return -1;
}
};
void print_vec(const vector<vector<int>>& vec){
for(const vector<int>& v: vec){
for(int e: v)
cout << e << " ";
cout << endl;
}
}
int main() {
vector<int> nums1 = {1, 0, -1, 0, -2, 2};
int target1 = 0;
print_vec(Solution().fourSum(nums1, target1));
return 0;
}