题目

Given an array nums of n integers and an integer target, are there elements a, b, c and d in nums such that a+ b + c + d = target ? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [1, 0 -1, 0, -2, 2], and target = 0.

A solution set is:

[

 [-1, 0, 0, 1],

 [-2, -1, 1, 2],

 [-2, 0, 0, 2]

]


思路

本题思路很简单,有了前面3Sum的基础,这里只要将 target-d,然后就是3Sum的解题方法。


C++

 vector<vector<int>> fourSum(vector<int>& nums, int target) {

        vector<vector<int> > result;
if(nums.size() < 4)
return result; sort(nums.begin(),nums.end()); int pMid = 0;
int pEnd = 0;
for(int i = 0;i<nums.size() - 3; i++){ //转化成3Sum问题
int subTarget=target - nums[i];
if(i > 0 && nums[i] == nums[i-1])
continue; for(int j = i + 1;j<nums.size()-2;j++){ int subTarget2 = subTarget - nums[j];
pMid = j + 1;
pEnd = nums.size() - 1;
if(j > i + 1 && nums[j] == nums[j-1])
continue; while(pMid < pEnd){
int sum1 = nums[pMid] + nums[pEnd]; if(sum1 < subTarget2){
pMid ++;
}
else if(sum1 > subTarget2){
pEnd --;
}
else{
vector<int> tempVec(4,0);
tempVec[0] = nums[i];
tempVec[1] = nums[j];
tempVec[2] = nums[pMid];
tempVec[3] = nums[pEnd]; result.push_back(tempVec); while(pMid < pEnd && nums[pMid] == nums[pMid+1]) //去除重复元素
pMid ++;
while(pMid < pEnd && nums[pEnd] == nums[pEnd -1])
pEnd --;
pMid ++;
pEnd --;
}
}
}
}
return result;
}

Python

18. 4Sum[M]四数之和的更多相关文章

  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] 454. 4Sum II 四数之和II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  3. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  4. [LeetCode] 454. 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  5. LeetCode(18):四数之和

    Medium! 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...

  6. 18 4Sum(寻找四个数之和为指定数的集合Medium)

    题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要 ...

  7. 力扣 ——4Sum (四数之和)python 实现

    题目描述: 中文: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 targe ...

  8. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  9. Java实现 LeetCode 18 四数之和

    18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...

随机推荐

  1. 全局变量变为局部变量 & MVC思想

    1 函数中的全局变量如何变成局部变量? 全局变量之间会相互骚扰.所以在代码中不要用全局变量.ES6之前只有函数里面有全局变量. 全局变成局部变量怎么变? 把代-放在一个函数如中,再.call()执行一 ...

  2. Codeforces Round #446

    Greed #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector&g ...

  3. 拖动盒子demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. java中参数传递实例

    //在函数中传递基本数据类型,            2. public class Test {         4.     public static void change(int i, in ...

  5. MeayunDB学习笔记(一) MeayunDB介绍及安装

    系列目录   MeayunDB介绍-高性能分布式内存数据库 MeayunDB学习笔记(一)MeayunDB介绍及安装 MeayunDB学习笔记(二)批量导入数据 MeayunDB学习笔记(三)索引应用 ...

  6. 写shell工具类,一个常用实例

    简述: 当我们常用到某些指令时,我们就需要将这个命令进行封装.封装的设计和扩展,因人而异.但为了每个人都能够了解到这个命令,常需要写出这个类的help. 关键字: 函数.getopts 函数 通过自定 ...

  7. SSH启动失败解决方法

    今天连接linux时居然连不上,报错信息是: 查了一下终于找到了解决办法,只需要一些命令 : cd /etc/ssh sudo chmod 644 ./* sudo chmod 600 ssh_hos ...

  8. JSCH实现文件上传下载至sftp服务器

    文件服务器采用FreeSSHd,文件服务器配置就不细说了. 直接上代码,该代码可以直接使用. import com.jcraft.jsch.*; import java.io.InputStream; ...

  9. CSS - 内联元素span 强制换行失败的可能原因

    在CSS中,标签span 强制换行失败:(使用display:block) 可能原因:float:left   or  float:right

  10. There are no packages available for installation. Sublime3解决方法

    最近在学习Vue,在配置sublime3的时候,想要高亮vue的语法,下载点插件 Package Control的时候,总报  There are no packages available for ...