题目描述:

中文:

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

英文:

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.

class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
nums = sorted(nums)
res = [] if not nums or len(nums) < 4:
return res if nums[0] + nums[1] + nums[2] + nums[3] > target:
return res if nums[-1] + nums[-2] + nums[-3] + nums[-4] < target:
return res for i in range(0, len(nums)):
if nums[i] + nums[-1] + nums[-2] + nums[-3] < target:
continue
for j in range(i + 1, len(nums) - 2):
if nums[i] + nums[j] + nums[-2] + nums[-1] < target:
continue
x = j + 1
y = len(nums) - 1
while x < y:
if nums[i] + nums[j] + nums[x] + nums[y] == target:
res.append([nums[i], nums[j], nums[x], nums[y]])
x = x + 1
while x < y and nums[x] == nums[x - 1]:
x = x + 1
elif nums[i] + nums[j] + nums[x] + nums[y] < target:
x = x + 1
else:
y = y - 1 rr = []
for r in res:
if r not in rr:
rr.append(r)
return rr

题目来源:力扣题库

力扣 ——4Sum (四数之和)python 实现的更多相关文章

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

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

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

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

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

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

  6. Java实现 LeetCode 18 四数之和

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

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

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

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

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

  9. Leetcode(18)-四数之和

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

  10. ACM_四数之和

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

随机推荐

  1. 两个对象值相同(x.equals(y)==true),hashcode也相同

    不对,如果两个对象x和y满足x.equals(y) == true,它们的哈希码(hash code)应当相同.Java对于eqauls方法和hashCode方法是这样规定的:(1)如果两个对象相同( ...

  2. 太恐怖了!黑客正在GPON路由器中利用新的零日漏洞

    即使在意识到针对GPONWi-Fi路由器的各种主动网络攻击之后,如果您还没有将其从互联网上带走,那么请小心,因为一个新的僵尸网络已加入GPON组织,该组织正在利用未公开的零日漏洞(零时差攻击). 来自 ...

  3. Python"sorted()"和".sort()"的区别

    sorted(A-LIST)会返回一个新的object,不改变**A-LIST*本身. A-LIST.sort()会直接改变A-List,不产生新的object.

  4. Laravel框架使用融云服务端SDK

    方法一:  使用第三方依赖安装(在项目根目录) 第一步:安装        composer require latrell/rongcloud dev-master 第二步:更新依赖包      c ...

  5. 多线程模拟生产者消费者示例之wait/notify

    public class Test { public static void main(String[] args) throws InterruptedException { List<Str ...

  6. PHP curl_file_create函数

    curl_file_create — 创建一个 CURLFile 对象. 说明 CURLFile curl_file_create ( string $filename [, string $mime ...

  7. linux系统下tomcat应用开机自启动 配置

    linux系统下tomcat应用开机自启动 配置 相对简单的方式是将tomcat添加为系统服务第一步  复制文件将 $Tomcat_Home/bin目录下的 catalina.sh脚本文件复制到目录/ ...

  8. LOJ 3094 「BJOI2019」删数——角标偏移的线段树

    题目:https://loj.ac/problem/3094 弱化版是 AGC017C . 用线段树维护那个题里的序列即可. 对应关系大概是: 真实值的范围是 [ 1-m , n+m ] :考虑设偏移 ...

  9. jmeter 自动生成测试报告命令

    环境要求 1:jmeter3.0版本之后开始支持动态生成测试报表 2:jdk版本1.7以上 3:需要jmx脚本文件 基本操作 1:在你的脚本文件路径下,执行cmd命令:jmeter -n -t tes ...

  10. 动态调试某个apk的smali代码,微信举例

    本地环境: PC:windows 10,Intellij IDEA (android studio应该一样的) 手机:nexus5 8.1系统, 其他依赖:smalidea插件,xposed 插件 h ...