题目描述:

中文:

给定一个包含 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. ZYNQ的Linux Linaro系统镜像制作SD卡启动(仅使用mkfs部分,其他部分待看)

    0. 概述 ZYNQ生成uboot的时候和正常的ARM设备不太一样,ZYNQ属于二次辅助启动uboot然后由uboot启动内核,大概意思就是 ZYNQ内部有一个机制,该机制不可修改,可以通过拨码开关控 ...

  2. javascaipt中数组元素删除方法delete和splice

    一.用delete 删除数组中元素(少用) 说明:delete: 只是被删除的元素变成了 undefined ,即数组长度不变 //定义数组 var array=["aa",&qu ...

  3. "sorted()"中的"Key Functions"

    Pythonsorted()函数中可以加入key=<FUNCTION>参数.作用是每个元素在排序之前,先作为key=<FUNCTION>中FUNCTION的参数,用FUNCTI ...

  4. hdu 5868:Different Circle Permutation 【Polya计数】

    似乎是比较基础的一道用到polya定理的题,为了这道题扣了半天组合数学和数论. 等价的题意:可以当成是给正n边形的顶点染色,旋转同构,两种颜色,假设是红蓝,相邻顶点不能同时为蓝. 大概思路:在不考虑旋 ...

  5. wangeditor 粘贴word内容带样式

    这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...

  6. 「NOI2017」蔬菜 解题报告

    「NOI2017」蔬菜 首先考虑流 可以从 \(s\) 流入表示得到蔬菜,流出到 \(t\) 表示卖出蔬菜,给每个蔬菜拆点,并给它它每天应得的蔬菜. 但是我们没办法直接给,注意到如果把变质看成得到并可 ...

  7. HDU 6069 Counting Divisors —— 2017 Multi-University Training 4

    Counting Divisors Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Oth ...

  8. 数据挖掘:WAP-Tree与PLWAP-Tree

    简介 我们首先应该从WAP-Tree说起,下面一段话摘自<Effective Web Log Mining using WAP Tree-Mine>原文 Abstract -World W ...

  9. postgresql创建表

    创建表时候有些方法:https://blog.csdn.net/qq_16605855/article/details/78905193

  10. Gym-100676F Palindrome

    原题连接:https://odzkskevi.qnssl.com/1110bec98ca57b5ce6aec79b210d2849?v=1491063604 题意: 多组输入,每一次输入一个n(字符串 ...