LeetCode18. 四数之和

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]


排序 + 三重循环 + 双链表

PHP代码:

function fourSum($nums, $target) {
sort($nums);
$ans = array();
for ($x = 0; $x < count($nums) - 3; ++ $x) {
for ($i = $x + 1; $i < count($nums) - 2; ++ $i) {
$j = $i + 1;
$k = count($nums) - 1;
while ($j < $k) {
$sum = $nums[$x] + $nums[$i] + $nums[$j] + $nums[$k];
if ($sum == $target) {
array_push($ans, array($nums[$x], $nums[$i], $nums[$j], $nums[$k]));
while ($j < $k and $nums[$j] == $nums[$j + 1]) ++ $j;
while ($j < $k and $nums[$k] == $nums[$k - 1]) -- $k;
++ $j;
-- $k;
}
elseif ($sum < $target) ++ $j;
else -- $k;
}
}
}
return array_unique($ans, 3);
}

LeetCode18. 四数之和的更多相关文章

  1. Leetcode12. 整数转罗马数字Leetcode18. 四数之和

    > 简洁易懂讲清原理,讲不清你来打我~ 输入整数,输出对应的罗马字符串![在这里插入图片描述](https://img-blog.csdnimg.cn/54b001c62a0d4d348c962 ...

  2. 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和

    [算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...

  3. [Swift]LeetCode18. 四数之和 | 4Sum

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

  4. LeetCode18.四数之和 JavaScript

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  5. leetcode18 四数之和 双指针

    题外话: 这道题让我想起了 挑战程序设计竞赛有一个抽签问题,类似的a+b+c+d=target,可以重复使用一个数. a+b+c+d=target转化为 a+b=target-c-d.  如果只是判断 ...

  6. LeetCode第十八题-四数之和

    4Sum 问题简介:定n个整数和整数目标的数组nums,是否有元素a,b,c,d在nums中,使a+b+c+d=target? 举例: 给定数组 nums = [1, 0, -1, 0, -2, 2] ...

  7. ACM_四数之和

    四数之和 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个不同的整数,判断能否从中选4次,4个数和刚好为m.数字可重复选取. ...

  8. [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 ...

  9. LeetCode:四数之和【18】

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

随机推荐

  1. luoguP4779 【模板】单源最短路径

    题目描述 单源最短路径模板. 使用 SPFA 肯定是不行的啦,网格图hack. 所以我们使用 Dijkstra 算法. 这里有一篇写的很好的 blog,无必要赘述.最后贴上代码. #include&l ...

  2. 自力更生Collections.sort发现比较结果混乱?Comparator的锅还是强转类型导致?

    近日开发任务时间充裕一些,于是有时间回顾一下项目. 我关注到了项目中使用的七牛云的对象存储服务. 作为测试需要上传了一些图片,但七牛的控制台却无法将内容按照上传时间排序或者是按照日期查询,由于buck ...

  3. gitlab 提交

    gitlab 提交 Git global setup git config --global user.name "lial" git config --global user.e ...

  4. .NET Core 3.0 里新的JSON API

    为什么需要新的JSON API? JSON.NET 大家都用过,老版本的ASP.NET Core也依赖于JSON.NET. 然而这个依赖就会引起一些版本问题:例如ASP.NET Core某个版本需要使 ...

  5. Redis 集群搭建(基于Linux)

    一.基础环境 1.虚拟机 VMware 15.x 2.Linux系统,用的是Centos7的Linux系统 3.Redis数据库版本 5.0.3 二.Redis集群简介 1.背景 Redis在3.0版 ...

  6. CSP考场Emacs使用指南[原创]

    前言: 据说,CSP考试,之后不再支持windows了呢. windows用户真得劲! 那用什么系统? Ubuntu上场了 Ubuntu编译指南 进入Ubuntu系统,在你想存的文件夹中新建一个空白文 ...

  7. linq一般用法

    最一般的用法 var rows = from c in dataTrue.AsEnumerable() from t in dataPre.AsEnumerable() ].ToString().St ...

  8. 【java基础】- java双亲委派机制

    在了解双亲委派机制之前,你应当知道classloader(如果不了解,可以现在去恶补一下哈) 四种classloader 虚拟机自带 引导类加载器(Bootstrap ClassLoader) 扩展类 ...

  9. 数据结构(四十六)插入排序(1.直接插入排序(O(n²)) 2.希尔排序(O(n3/2)))

    一.插入排序的基本思想 从初始有序的子集合开始,不断地把新的数据元素插入到已排列有序子集合的合适位置上,使子集合中数据元素的个数不断增多,当子集合等于集合时,插入排序算法结束.常用的 插入排序算法有直 ...

  10. APP打包设置程序版本号

    正确设置方式是: 注意,以下修改不会起作用<manifestxmlns:android="http://schemas.android.com/apk/res/android" ...