这里我们可以考虑将 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. Spring整合rabbitmq(转载)

    原文地址:https://my.oschina.net/never/blog/140368 1.首先是生产者配置 <?xml version="1.0" encoding=& ...

  2. Java集合--Map总结

    转载请注明出处:http://www.cnblogs.com/skywang12345/admin/EditPosts.aspx?postid=3311126 第1部分 Map概括 (01) Map ...

  3. Java集合--Map架构

    概要 前面,我们已经系统的对List进行了学习.接下来,我们先学习Map,然后再学习Set:因为Set的实现类都是基于Map来实现的(如,HashSet是通过HashMap实现的,TreeSet是通过 ...

  4. stm32如何核对flash大小和sram大小

    以stm32f103zet6为例,直接上图:

  5. C# 任务、线程、同步(一)

    static object taskMethodLock = new object(); static void TaskMethod(object title) { lock (taskMethod ...

  6. POJ3336 Making the Grade

    思路:DP 提交:1次 题解: 最开始我们可以想到,分两种序列都做一遍. 先来证明一个结论: 存在一种构造,使 \(B\) 中的数都在 \(A\) 中出现过,且这样不劣. (目的是为了转化暂时看起来虚 ...

  7. [Luogu] 校园网Network of Schools

    https://www.luogu.org/problemnew/show/2746 Tarjan 缩点 判断入度为0的点的个数与出度为0的点的个数的关系 注意全缩为一个点的情况 #include & ...

  8. Java分布式互联网架构/微服务/高性能/springboot/springcloud 2018年10月17日直播内容

    2018年10月17日直播内容 大规模并发必备的消息中间件技术ActiveMq 网盘链接: https://pan.baidu.com/s/1GlxsZ2JnrvX- YN16-S7lQw 提取码: ...

  9. Ubuntu14.04 gzip failed file too large

    使用gzip解压一个oracle rman备份集时报错:File too large.gizp -d cosp_db_full.tar.gzgzip: cosp_db_full.tar:File to ...

  10. dosbox+masm汇编环境的安装和使用

    1. 下载dosbox安装程序:DOSBox0.74-win32-installer.exe 链接:https://pan.baidu.com/s/1gXPKTT-xKb6BpjOJdhmudA 密码 ...