一.题目链接: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. directive例子2

    (function() { angular.module('app.widgets') .directive('bsModalPlus', function($window, $sce, $modal ...

  2. Bi-shoe and Phi-shoe

    欧拉函数中的性质 Φ(p)=p-1,p为素数.所以这个题算是贪心+数论吧.每个Φ(p)=p-1:只要从p开始,找素数,那么一定有Φ(k)>=p-1;只有当p=k时,等号成立. #include ...

  3. P2157 [SDOI2009]学校食堂 (dp+状态压缩)

    题目链接:传送门 题目: 题目描述 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜肴.当然,不同的人口味也不一定相同,但每个人 ...

  4. chmod、chown、umask、lsattr/chattr

    1.chmod 命令 改变文件权限 文件对于使用者来说,有 读 .写 .执行 (当然,还有删除),而这里主要说的是,读写执行(rwx)    r  w  x    对应的是 读写执行,也对应 :  4 ...

  5. 容器(container)

    一.容器有哪些 平时我们经常看到各种容器名称:Servlet容器.WEB容器.Java WEB容器.Java EE容器等 二.容器是什么 2-1     容器通常理解就是装东西的,我们这里说技术上的容 ...

  6. ios-密码加密

    加密文件可到网上搜索MyMD5后下载 MyMD5.h文件 // // MyMD5.h // GoodLectures // // Created by yangshangqing on 11-10-1 ...

  7. Js判断字符的种类

    Js判断字符的种类:unicode范围: 48-57:0-9    数字字符 65-90:A-Z    大写字母 97-122: a-z  小写字母 19968-40869:汉字 其他字符 实例:输出 ...

  8. MySQL 5.7--多源复制(非GTID模式)

    ==================================================== 在MYSQL5.7版本中引入多源复制,一个从库允许复制多个主库的数据,每个主库被配置为一个单独 ...

  9. 使用 loki grafana 分析nginx 请求日志

    loki 是类似prometheus 的log 可视化展示.收集系统,已经集成在grafana 6.0 版本中了 说明: 测试环境使用了docker-compose 运行 环境准备 docker-co ...

  10. hasura graphql-engine v1.0.0-alpha30 功能试用

    hasura graphql-engine v1.0.0-alpha30 有好多新的功能的添加 环境准备 docker-compose 文件 version: '3.6' services: post ...