题目链接:18.四数之和

题解:

又是使用双指针的题,双指针可以将复杂度降低一维!

这道题和LeetCode的第15题完全一样!我的第15题链接

题目简述:

给定一个nums数组,需要求出所有相加为target的四元组,并且要求不包含重复四元组!

题解:

当然可以使用暴力,嗯,,四重循环,没试过,可能会超时!

使用呢双指针将四维降到三维度,固定前两个数,后两个数使用双指针移动!

题解:参考我写的LeetCode第15题的题解。具体做法,判重,注意事项,完全一样!

时间复杂度: 第一层循环O(N),第二层O(N),第三层看似O(N ^ 2),实则lr各最多扫描N次,所以最终复杂度为O(N ^ 3)

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i ++){
if(i && nums[i] == nums[i - 1]) continue;
for(int j = i + 1; j < nums.size(); j ++){
if(j > i + 1 && nums[j] == nums[j - 1]) continue;
for(int l = j + 1, r = nums.size() - 1; l < r; l ++){
if(l > j + 1 && nums[l] == nums[l - 1]) continue;
while(l < r - 1 && nums[i] + nums[j] + nums[l] + nums[r] > target) r --;
if(nums[i] + nums[j] + nums[l] + nums[r] == target){
res.push_back({nums[i], nums[j], nums[l], nums[r]});
}
}
}
}
return res;
}
};