Skip to content

Commit 7be49cc

Browse files
authored
Update 4sum.cpp
1 parent 39cc704 commit 7be49cc

File tree

1 file changed

+15
-53
lines changed

1 file changed

+15
-53
lines changed

C++/4sum.cpp

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,31 @@
33

44
class Solution {
55
public:
6-
vector<vector<int> > fourSum(vector<int> &num, int target) {
7-
int len = num.size();
8-
int left, right, sum;
9-
sort(num.begin(), num.end());
10-
vector<vector<int>> res;
11-
for (int i = 0; i < len - 3; ++i) {
12-
if (i && num[i] == num[i - 1]) {
6+
vector<vector<int> > fourSum(vector<int> &nums, int target) {
7+
sort(begin(nums), end(nums));
8+
vector<vector<int>> result;
9+
for (int i = 0; i + 3 < size(nums); ++i) {
10+
if (i && nums[i] == nums[i - 1]) {
1311
continue;
1412
}
15-
for (int j = i + 1; j < len - 2; ++j) {
16-
if (j != i + 1 && num[j] == num[j - 1]) {
13+
for (int j = i + 1; j + 2 < size(nums); ++j) {
14+
if (j != i + 1 && nums[j] == nums[j - 1]) {
1715
continue;
1816
}
19-
sum = target - num[i] - num[j];
20-
left = j + 1, right = len - 1;
17+
int total = target - nums[i] - nums[j];
18+
int left = j + 1, right = size(nums) - 1;
2119
while (left < right) {
22-
if (num[left] + num[right] == sum) {
23-
res.push_back({num[i], num[j], num[left], num[right]});
20+
if (nums[left] + nums[right] == total) {
21+
result.push_back({nums[i], nums[j], nums[left], nums[right]});
2422
++left, --right;
25-
while (left < right && num[left] == num[left - 1]) {
23+
while (left < right && nums[left] == nums[left - 1]) {
2624
++left;
2725
}
28-
while (left < right && num[right] == num[right + 1]) {
26+
while (left < right && nums[right] == nums[right + 1]) {
2927
--right;
3028
}
3129
} else {
32-
if (num[left] + num[right] > sum) {
30+
if (nums[left] + nums[right] > total) {
3331
--right;
3432
} else {
3533
++left;
@@ -38,42 +36,6 @@ class Solution {
3836
}
3937
}
4038
}
41-
return res;
42-
}
43-
};
44-
45-
// Time: O(n^4)
46-
// Space: O(n^2)
47-
class Solution2 {
48-
public:
49-
vector<vector<int> > fourSum(vector<int> &num, int target) {
50-
vector<vector<int>> ans;
51-
if (num.size() < 4) {
52-
return ans;
53-
}
54-
sort(num.begin(), num.end());
55-
unordered_multimap<int, pair<int, int>> cache;
56-
57-
for (int i = 0; i < num.size(); ++i) {
58-
for (int j = i + 1; j < num.size(); ++j) {
59-
cache.emplace(num[i] + num[j], make_pair(i, j));
60-
}
61-
}
62-
63-
for (auto i = cache.begin(); i != cache.end(); ++i) {
64-
auto a = i->second.first;
65-
auto b = i->second.second;
66-
auto range = cache.equal_range(target - i->first);
67-
for (auto j = range.first; j != range.second; ++j) {
68-
auto c = j->second.first;
69-
auto d = j->second.second;
70-
if (b < c) {
71-
ans.push_back({num[a], num[b], num[c], num[d]});
72-
}
73-
}
74-
}
75-
sort(ans.begin(), ans.end());
76-
ans.erase(unique(ans.begin(), ans.end()), ans.end());
77-
return ans;
39+
return result;
7840
}
7941
};

0 commit comments

Comments
 (0)