• 作者: 负雪明烛
  • id: fuxuemingzhu
  • 个人博客:http://fuxuemingzhu.cn/
  • 个人公众号:负雪明烛
  • 本文关键词:four sum, 4sum, 四数之和,题解,leetcode, 力扣,Python, C++, Java

题目地址:https://leetcode.com/problems/4sum/description/

题目描述

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]
]

题目大意

找出一个数组中所有的和为target的四个数字,要求返回的结果里面不准有重复的四元组。

解题方法

遍历

总结一下 K-sum 题目。

  1. 首先先排序
  2. 然后用

    K

    2

    K - 2

    K−2 个指针做 O(N^(K - 2)) 的遍历

  3. 剩下 2 个指针从第 2 步的剩余区间里面找,找的方式是使用两个指针 p, q 分别指向剩余区间的首尾,判断两个指针的和与 target - 第2步的和的关系,对应的移动指针。即如果两个数的和大了,那么, q–;如果两个数的和小了,那么,p++;等于的话,输出结果。要时刻注意 p < q.
  4. 用 p,q 查找剩余区间结束之后,需要移动前面的K-2个值,这里需要在移动的过程中做个去重,找到和前面不同的值继续查找剩余区间。

时间复杂度是O(N^3),空间复杂度是O(1).

class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
N = len(nums)
nums.sort()
res = []
i = 0
while i < N - 3:
j = i + 1
while j < N - 2:
k = j + 1
l = N - 1
remain = target - nums[i] - nums[j]
while k < l:
if nums[k] + nums[l] > remain:
l -= 1
elif nums[k] + nums[l] < remain:
k += 1
else:
res.append([nums[i], nums[j], nums[k], nums[l]])
while k < l and nums[k] == nums[k + 1]:
k += 1
while k < l and nums[l] == nums[l - 1]:
l -= 1
k += 1
l -= 1
while j < N - 2 and nums[j] == nums[j + 1]:
j += 1
j += 1 # 重要
while i < N - 3 and nums[i] == nums[i + 1]:
i += 1
i += 1 # 重要
return res

相似题目

Two Sum
Two Sum II - Input array is sorted
15. 3Sum
16. 3Sum Closest
923. 3Sum With Multiplicity
454. 4Sum II

参考资料

https://blog.csdn.net/MebiuW/article/details/50938326

日期

2018 年 10 月 30 日 —— 啊,十月过完了

【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四数之和

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

  3. 【LeetCode 18】四数之和

    题目链接 [题解] 两重循环枚举[i..j]这个区间 同时规定必取nums[i]和nums[j] 那么现在的问题就变成在下标为[i..j]这个区间的数字里面找两个数字使他们的和为target-nums ...

  4. Leetcode(18)-四数之和

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

  5. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  6. [LeetCode] 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. [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 ...

  8. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  9. Java实现 LeetCode 18 四数之和

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

随机推荐

  1. DNS域名解析全过程

    一张图看懂DNS域名解析全过程   DNS域名解析是互联网上非常重要的一项服务,上网冲浪(还有人在用这个词吗?)伴随着大量DNS服务来支撑,而对于网站运营来说,DNS域名解析的稳定可靠,意味着更多用户 ...

  2. Oracle-怎么在表的特定位置增加列

    create table tmp as select ID,UserName,RealName,Sex,Tel,Addr from tabName;drop table tabName;rename ...

  3. FFmpeg笔记:使用MSVC工具链编译Windows版本静态库、动态库

    2019年3月开始,为了将音视频编解码功能集成到Cocos2d-x中,开始接触到FFmpeg: 当时开发环境还在Mac下,编译FFmpeg相比现在用Windows平台要方便的多: 最近,公司内部有个U ...

  4. python APScheduler模块

    简介 一般来说Celery是python可以执行定时任务, 但是不支持动态添加定时任务 (Django有插件可以动态添加), 而且对于不需要Celery的项目, 就会让项目变得过重. APSchedu ...

  5. A Child's History of England.27

    Then, the Red King went over to Normandy, where the people suffered greatly under the loose rule of ...

  6. IT四大名著

    标题耸人听闻,sorry. CPU.操作系统.编译器和数据库我都不会.我英语也不行,但我认识所有的字母.:-) 万一有人感兴趣呢?https://sqlite.org/doclist.htmlThe ...

  7. Azkaban(一)【集群安装】

    目录 一.下载解压 二. 配置Mysql 三. 配置Azkaban Executor 四. 配置Azkaban WebServer 一.下载解压 1.下载地址:https://github.com/a ...

  8. Linux系统中安装软件方法总结

    Linux系统中安装软件方法总结 [1]Linux系统中安装软件的几种方式 [2] Linux配置yum源(本地源和网络源) [3] SuSE下zypper源配置 [4] SUSE zypper 本地 ...

  9. HelloWorldMBean

    package mbeanTest; public interface HelloWorldMBean { public String getHello(); public void setHello ...

  10. 【Linux】【Shell】【Basic】条件测试

    1. 数值测试:数值比较 -eq:是否等于: [ $num1 -eq $num2 ] -ne:是否不等于: -gt:是否大于: -ge:是否大于等于: -lt:是否小于: -le:是否小于等于: 2. ...