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. Web前端安全之iframe

    防嵌套网页 比如,最出名的clickhacking就是使用iframe来 拦截click事件.因为iframe享有着click的最优先权,当有人在伪造的主页中进行点击的话,如果点在iframe上,则会 ...

  2. Display 1 No cameras rendering

    解决: 点击Main Camera ,

  3. Ubuntu 设置默认以Root用户身份登录

    系统 :Linux ubuntu 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 G ...

  4. [BZOJ1694/1742/3074]The Cow Run 三倍经验

    Description John养了一只叫Joseph的奶牛.一次她去放牛,来到一个非常长的一片地,上面有N块地方长了茂盛的草.我们可 以认为草地是一个数轴上的一些点.Joseph看到这些草非常兴奋, ...

  5. redis内存数据的持久化方式

    转: http://blog.csdn.net/wzqzhq/article/details/64920996 概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis ...

  6. Linux与Git学习笔记

    Linux基础概念篇: 终端.控制器 命令行界面 (CLI).终端 (Terminal).Shell.TTY的区别 Linux下的yum与apt-get Linux中su.su -和sudo的区别 L ...

  7. postman-windows下newman的使用

    一.newman的安装 1.安装node.js下载https://nodejs.org/en/ C:\Users\iphauser>node -vv10.16.1C:\Users\iphause ...

  8. 基于mysql-8.0.16-winx64的主从搭建

    1.主服务器的my.ini文件内容:[mysqld]# 主库和从库需要不一致server-id=1log-bin=mysql-bin# 同步的数据库binlog-do-db=master-slave# ...

  9. win7更新,360手机安装谷歌框架

    这两天把11平台被卸载了,不能打竞技场了,很伤心. 成年男子,总要找点有趣的事情去做.我准备洗心革面,好好学习.(巴拉巴拉巴拉一万字.) 首先第一件事情就是重装系统,(由于买了个假显卡,win10以上 ...

  10. (记录)Ubuntu系统中运行需要导入jar包的Java程序

    在学习Redis的过程中,在学到Redis客户端Jedis的时候,考虑到能不能在ubuntu下用Vim编写Java程序并且能够运行呢? 于是,首先在网上调研了一番用Vim写Java程序的可实现性. 相 ...