【LeetCode】18. 4Sum 四数之和
- 作者: 负雪明烛
- 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 题目。
- 首先先排序
- 然后用
K
−
2
K - 2
K−2 个指针做
O(N^(K - 2))的遍历 - 剩下 2 个指针从第 2 步的剩余区间里面找,找的方式是使用两个指针 p, q 分别指向剩余区间的首尾,判断两个指针的和与 target - 第2步的和的关系,对应的移动指针。即如果两个数的和大了,那么, q–;如果两个数的和小了,那么,p++;等于的话,输出结果。要时刻注意 p < q.
- 用 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 四数之和的更多相关文章
- [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 ...
- [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 ...
- 【LeetCode 18】四数之和
题目链接 [题解] 两重循环枚举[i..j]这个区间 同时规定必取nums[i]和nums[j] 那么现在的问题就变成在下标为[i..j]这个区间的数字里面找两个数字使他们的和为target-nums ...
- Leetcode(18)-四数之和
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- [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 ...
- [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 ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...
随机推荐
- R语言与医学统计图形-【26】ggplot2主题函数
ggplot2绘图系统--主题函数 1. theme函数 theme_*系列函数提供了9种不同的风格. theme_grey/gray/bw/linedraw/light/minimal/classi ...
- rust Option枚举
枚举 1 fn main() { 2 let a_binding; 3 { 4 let x = 2; 5 a_binding = x * x; 6 } 7 println!("a bindi ...
- nginx二级域名指向不同文件项目配置
需要使用泛域名解析, 并且加上空的判断,以保证没有二级域名的也可以访问 核心配置 server_name ~^(?<subdomain>.+)\.caipudq\.cn$;if ( $su ...
- A Child's History of England.50
'Knave [man without honor]!' said King Richard. 'What have I done to thee [you] that thou [you] shou ...
- Vue相关,vue父子组件生命周期执行顺序。
一.实例代码 父组件: <template> <div id="parent"> <child></child> </div& ...
- 网口程序udp
# -*- coding: utf-8 -*- """ Created on Thu Nov 12 15:02:53 2020 @author: Administrato ...
- Python3的类注意事项
参考: https://www.runoob.com/python/python-object.html https://www.runoob.com/w3cnote/python-extends-i ...
- Shell学习(四)——shell中各种括号的作用
参考博客: [1]shell中各种括号的作用().(()).[].[[]].{} [2]shell中的单层大/中/小括号.双层大中小括号.命令替换等 一.前言 目录 单括号() 双括号(( )) 单中 ...
- ORACEL 创建DIRECTORY
oracle要直接对文件进行读写必须先创建一个DIRECTORY. 语法如下: CREATE DIRECTORY UTL_FILE_DIR AS '/home/oracle/oradir'; 可以通过 ...
- Linux学习 - 压缩解压命令
一." .gz "压缩文件 1 压缩语法 gzip [文件] 2 解压语法 gunzip [压缩文件] 3 注 gzip只能压缩文件 gzip不保留原文件 二." . ...