leetcode 四数之和
这里我们可以考虑将 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 四数之和的更多相关文章
- [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:四数之和【18】
LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- LeetCode:两数之和、三数之和、四数之和
LeetCode:两数之和.三数之和.四数之和 多数之和问题,利用哈希集合减少时间复杂度以及多指针收缩窗口的巧妙解法 No.1 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在 ...
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- LeetCode第十八题-四数之和
4Sum 问题简介:定n个整数和整数目标的数组nums,是否有元素a,b,c,d在nums中,使a+b+c+d=target? 举例: 给定数组 nums = [1, 0, -1, 0, -2, 2] ...
- 【LeetCode】四数之和
[问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...
- 【LeetCode】18.四数之和
题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...
随机推荐
- cas的客户端配置
知识点:cas的客户端配置 一:cas客户端配置 二:cas认证流程原理(图) 参考:https://www.cnblogs.com/suiyueqiannian/p/9359597.html 源码: ...
- 2018年5月20日--西安icpc邀请赛试题一览
热身赛 正式赛 A题,样例不代表后台数据,出题人把题意和后台数据代表的意思搞差了! B: C: D-E F f-G G G-H H-I I-J J-k K-2
- go语言的defer语句
转: https://www.jianshu.com/p/5b0b36f398a2 ---------------------------------------------------------- ...
- JavaScript005,语法
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- .npmrc 实用小技巧
小技巧 因为每次执行 npm adduser 的时候都需要输入用户名.密码和email 很麻烦,我们都可以配置在.npmrc 文件中,在命令行中执行如下脚本 echo -n 'myuser:mypas ...
- 慕课网SSM仿大众点评
目录: 配置部分: 1 配置报错不支持diamond运算符 运行部分: 1 登录的账号密码 2 运行项目是报错session超时 配置部分 1 配置报错不支持diamond运算符 原报错信息如下:id ...
- Shell 脚本来自动监控 Linux 系统的内存
# vim /scripts/swap-warning.sh #!/bin/bash #提取本服务器的IP地址信息 IP=`ifconfig eth0 | grep "inet addr&q ...
- 023_统计当前 Linux 系统中可以登录计算机的账户有多少个
#!/bin/bash #方法 1: grep "bash$" /etc/passwd | wc -l #方法 2: #-F END都要大写! awk -F: '/bash$/{x ...
- 数据结构实验之图论八:欧拉回路(SDUT 3364)
Problem Description 在哥尼斯堡的一个公园里,有七座桥将普雷格尔河中两个岛及岛与河岸连接起来. 能否走过这样的七座桥,并且每桥只走一次?瑞士数学家欧拉最终解决了这个问题并由此创立了拓 ...
- re匹配 [\s\S][\w\W]的使用.
本来想提取一个字符串写了一堆正则都提取不出来. 因为有特殊字符 后来使用 [\s\S]* 或 [\w\W]* 匹配出来. \s 空白字符 [ \t\n\r\f\v] \S 非空白字符 相当于 [^ \ ...