一.题目链接: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. cboss升级顺序

    1. sunboss去除cron.d ps.运营商cboss安装说明: 1. 先安装管理服务器,管理服务器安装所有包,安装顺序如下: (1)cdb20 (2)cboss (3)db (4)boss ( ...

  2. 编译Thrift支持golang

    本文已经是很久以前的文章了,也不知道新版本thrift如何 Thrift是一个跨语言的服务部署框架,Thrift通过一个中间语言(IDL, 接口定义语言)来定义RPC的接口和数据类型,然后通过一个编译 ...

  3. XXS level3

    (1)用level1和2的方法都行不通,查看PHP源代码,发现url与输入框内容都进行了过滤 <?php ini_set("display_errors", 0); $str ...

  4. HPU组队赛J:Ball King(线段树)

    时间限制 1 Second  内存限制 512 Mb 题目描述 HPU601球王争霸赛即将举行,ACMER纷纷参加. 现在有n个人报名参赛,每个人都有一个实力值 ai,实力值较大者获胜. 为保证比赛公 ...

  5. 企业wiki之confluence安装部署(linux)及其破解

    系统环境(虚拟机) centos6.5   2G运行内存,30g硬盘,cpu最好也分配两个或多个,因为我在安装过程中发现很卡,cpu占用率几乎占满 需要用到的安装包和文件可以在这里找 链接:https ...

  6. mino 路径格式的bucket 数据访问

    实施上这个功能很简答,如果官方不支持,我们可以通过基于nginx 的url rewrite 也可以实现 格式说明 如果配置了domain minio 会将 http://mydomain.com/bu ...

  7. 使用patroni 构建高可用的pg 数据库

    patroni 是一个基于zk.etcd .consul 等的pg ha 模版,我们可以使用这个工具,快速的搭建一套 pg 的高可用方案 环境准备 mac 操作系统 安装基础差组件 brew inst ...

  8. PowerDesigner基础使用 ---- 入门学习

    1:入门级使用PowerDesigner软件创建数据库(直接上图怎么创建,其他的概念知识可自行学习) 我的PowerDesigner版本是16.5的,如若版本不一样,请自行参考学习即可.(打开软件即是 ...

  9. js里面的全局属性 全局对象 全局函数

    1)全局属性 Infinity   typeof Infinity        //number NaN typeof NaN           //number undefined       ...

  10. VBA: 怎样批量数据从Excel派出到Visio

    上周派到了个case, 是批量从Excel导出数据导Visio每个图形中. 花了些时间实现了这个功能. 原理如下: 打开Excel 新建/打开表单 指向所选择的表单 遍历所在列的所有数据 打开Visi ...