这里我们可以考虑将 n 数之和降低为一个数加上 n-1 数之和的问题。依次降低 ,最低是二数之和的问题 ,二数之和问题容易解决。
主要在于从 n 到 n-1 的过程需要理解 :下列代码中前几个 if 是对特殊情况的处理 ;通过新的 target = target - nums[i] 进行参数修改再次调用 nsum()方法即递归 。

def nsum(nums, target, n, result, results):  #求n数之和=target
if len(nums) < n or n < 2 or target < nums[0]*n or target > nums[-1]*n:
return []
if n==2:
begin,end = 0,len(nums)-1
while begin<end:
sums = nums[begin]+nums[end]
if sums<target:
begin += 1
elif sums>target:
end -=1
else:
plet = [nums[begin],nums[end]]
results.append(result+plet)
while begin<end and nums[begin] ==plet[0]:begin += 1 #重复数字跳过
while begin<end and nums[end] == plet[1]:end -=1 #重复数字跳过
else:
for i in range(len(nums)-n+1):
if (i>0 and nums[i]==nums[i-1])or(nums[i]+(n-1)*nums[len(nums)-1]<target):
continue
if n*nums[i]>target:
break
if n*nums[i] == target and i+n-1 <len(nums) and nums[i+n-1] == nums[i]:
plet = [nums[i]]*n
results.append(result+plet)
break
else:
nsum(nums[i+1:],target-nums[i],n-1,result+[nums[i]],results) #递归,这里注意第一个参数是nsum[i+1:],敲代码易敲错为nsum[i+1] if __name__ == '__main__':
results = []
nums = [1, 0, -1, 0, -2, 2]
nums.sort()
target = 0
nsum(nums,target,4,[],results) #n=4,即求4数之和=0(target)
print(results)

leetcode 四数之和的更多相关文章

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

  2. LeetCode:四数之和【18】

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

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

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

  4. LeetCode:两数之和、三数之和、四数之和

    LeetCode:两数之和.三数之和.四数之和 多数之和问题,利用哈希集合减少时间复杂度以及多指针收缩窗口的巧妙解法 No.1 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在 ...

  5. Java实现 LeetCode 18 四数之和

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

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

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

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

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

  8. 【LeetCode】四数之和

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

  9. 【LeetCode】18.四数之和

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

随机推荐

  1. Web应用中的缓存一致性问题

    上篇总结了缓存中出现频率比较高的一些问题,今天详细说说web应用中的缓存一致性问题. 主要说以下三个方面 数据库与缓存中数据不一致出现的情形 发生不一致时的优化思路 如何保证数据库与缓存的一致性 先来 ...

  2. TCP中的长连接和短连接(转载)

    原文地址:http://www.cnblogs.com/onlysun/p/4520553.html 次挥手,所以说每个连接的建立都是需要资源消耗和时间消耗的  示意图:               ...

  3. Windows服务 System.ServiceProcess.ServiceBase类

    一.Windows服务 1.Windows服务应用程序是一种需要长期运行的应用程序,它适合服务器环境. 2.无用户界面,任何消息都会写进Windows事件日志. 3.随计算机启动而启动,不需要用户一定 ...

  4. Vue结合后端DjangoFramework的在线生鲜超市(前后端分离)【django2.2+xadmin+ueditor】

    在线博客教程:https://www.cnblogs.com/Eric15/category/1300432.html https://www.cnblogs.com/derek1184405959/ ...

  5. 2018 南京网络预赛Sum - 线性筛

    题意 链接 定义 $f(x)$ 为满足以下条件的有序二元组 $(a, b)$ 的方案数(即 $(a, b)$ 与 $(b, a)$ 被认为是不同的方案): $x= ab$ $a$ 和 $b$ 均无平方 ...

  6. 2019EC-Final参赛总结

    本来想发知乎上的,后来发现太长就放这好了23333 没写过这种东西,所以写得比较混乱&流水账 以下内容均为我的个人视角XD   赛前 在车上的时候,红太阳跟我们说他头晕(虽然他好像每场比赛都头 ...

  7. 25、自动装配-@Profile根据环境注册bean

    25.自动装配-@Profile根据环境注册bean 指定组件在哪个环境的情况下才能被注册到容器中 加了环境标识的,只有这个环境被激活才能注册到组件中 默认是default环境 写在类上,整个配置类的 ...

  8. MySQL建立索引的原则

    1.表的主键.外键必须有索引; 2.数据量超过300的表应该有索引; 3.经常与其他表进行连接的表,在连接字段上应该建立索引; 4.经常出现在Where子句中的字段,特别是大表的字段,应该建立索引; ...

  9. Ubuntu: error: snap “phpstorm” has “install-snap” change in progress

    Ubuntu: error: snap “phpstorm” has “install-snap” change in progress 投稿日 : 2019-06-10 | カテゴリー : linu ...

  10. E.Substring Reverse Gym - 101755E

    Substring Reverse Problem Two strings s and t of the same length are given. Determine whether it is ...