一.题目链接:https://leetcode.com/problems/4sum/

二.题目大意:

 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数的和等于target,找出所有的四元组,当然这些四元组不能有重复的。

三.题解:

这道题实质就是3sum的变形,关于4sum问题,已经在https://www.cnblogs.com/wangkundentisy/p/9079622.html这里说过了,无外乎最外面两层循环,最里面的循环使用哈希表或者双指针,此处使用的是双指针法。具体代码如下:

class Solution
{
public:
vector<vector<int>> fourSum(vector<int>& nums, int target)
{
int len = nums.size();
vector<vector<int>> rs;
if(len < 4)
return rs;
sort(nums.begin(),nums.end());
for(int i = 0; i < len; i++)
{
if(i != 0 && nums[i] == nums[i - 1])
continue;
for(int j = i + 1; j < len - 1; j++)
{
if(j != i + 1 && nums[j] == nums[j -1])//注意此处的去重,要保证j不是第一次被使用,所以必须是j!=i+1
continue;
int l = j + 1, r = len - 1;
while(l < r)
{
if(nums[i] + nums[j] + nums[l] + nums[r] == target)
{
rs.push_back({nums[i],nums[j],nums[l],nums[r]});
l++;
r--;
while( l < r && nums[l] == nums[l - 1])
l++;
while(l < r && nums[r] == nums[r + 1])
r--;
}
else if(nums[i] + nums[j] + nums[l] + nums[r] < target)
l++;
else
r--; }
}
}
return rs; }
};

本例中的方法的时间复杂度为O(N^3),空间复杂度为O(1)。此外,之前在3sum问题中总结过:4sum问题还有时间复杂度为O(N^2)的算法,具体可见:https://www.cnblogs.com/wangkundentisy/p/9079622.html

这里有几点需要注意:

1.由于题目要求四元组是唯一的,所以要进行去重:每一个外层循环都需要去重,即i和j都要去重(对于ksum问题,也是这样的,k-1个外层循环都要按照这个规律进行去重)

2.外层循环的去重,要保证此处的指针不是第一次被使用,也就是说在该位置之前,应该确保已经执行过一次了(如果是第一次使用,就不用去重了),所以才用 i != 0和 j != i + 1这种判断条件,对于ksum问题,一定注意这种规律。

LeetCode——18. 4Sum的更多相关文章

  1. [LeetCode] 18. 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  2. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  3. [LeetCode] 18. 4Sum ☆☆

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  4. LeetCode 18. 4Sum (四数之和)

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  5. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

  6. Leetcode 18. 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  7. Java [leetcode 18]4Sum

    问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...

  8. C#解leetcode 18. 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  9. [leetcode]18. 4Sum四数之和

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

随机推荐

  1. 安装Ubuntu16.04与windows10双系统后,如何修改启动默认设置

    在安装了Ubuntu16.04系统之后,系统会默认自启动Ubuntu16.04,而我们大多数情况下可能都在使用windows系统,不修改默认设置,不经意间便会启动了Ubuntu16.04,通过我的经历 ...

  2. 《DSP using MATLAB》Problem 5.28

    昨晚手机在看X信的时候突然黑屏,开机重启都没反应,今天维修师傅说使用时间太长了,还是买个新的吧,心疼银子啊! 这里只放前两个小题的图. 代码: 1. %% ++++++++++++++++++++++ ...

  3. Js 判断输入的验证码是否一致

    实现效果: 判断输入的验证码是否一致 如果不同,alert出验证码输入有误~, 输入正确输出登录成功. <!DOCTYPE html> <html lang="en&quo ...

  4. 重拾C++第一天_WDS

    1.面向对象编程的三大特点:封装.继承.多态 2.C++中若不指定类中成员的访问权限默认就是private的(class默认是private的,struct默认是public的). 3.C++规范中类 ...

  5. MySQL Inport--导入数据

    LOAD DATA INFILE导入数据 语法: LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE '' [REPLACE | IGNORE] ...

  6. tailor multi fragment && cutom-amd script demo 说明

    tailor 官方demo 中提供了一个multi fragment 的demo,这个比较简单,就是使用不同的 后端server 做为fragment ,然后使用 html tag 进行加载就可以了. ...

  7. PostgreSQL Q&A: Building an Enterprise-Grade PostgreSQL Setup Using Open Source Tools

    转自:https://www.percona.com/blog/2018/10/19/postgresql-building-enterprise-grade-setup-with-open-sour ...

  8. Complete Physics Platformer Kit 学习

    using UnityEngine; /// <summary> /// 摄像机跟随玩家 /// </summary> public class CameraFollow : ...

  9. S老师 C#编程数据结构篇 学习

    直接插入排序                                                       冒泡排序 简单选择排序 线性表: using System; using Sy ...

  10. matlab:Source Control Integration

    http://cn.mathworks.com/help/matlab/source-control.html