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 ...
随机推荐
- vue3.0脚手架 创建项目
1.下载node最新稳定版本,并且安装 2.安装好之后,在cmd或者terminal下, 使用npm -v 查看当前npm版本,验证是否安装成功 3.安装成功后,运行 npm i -g @vue/cl ...
- vulkan的subpass
最近在写 unity上 vulkan开subpass 似乎pc上subpass 的input attachement hlslcc_fbinput_0绑不上的 在手机上能绑上 说明subpass这个功 ...
- 2019HDU多校第三场 K subsequence——最小费用最大流
题意 给定一个 $n$ 个整数的数列,从中至多选取 $k$ 个上升子序列(一个元素最多被选一次),使得选取的元素和最大. 分析 考虑这个问题和经典网络流问题“最长不下降子序列”相似,我们考虑对这个建图 ...
- BZOJ 4129 Haruna’s Breakfast ( 树上带修莫队 )
题面 求树上某路径上最小的没出现过的权值,有单点修改 添加链接描述 分析 树上带修莫队板题,问题是怎么求最小的没出现过的权值. 因为只有nnn个点,所以没出现过的最小值一定在[0,n][0,n][0, ...
- [Functional Programming] liftA2 and converge
Sometimes I am confused with 'liftA2' and 'converge' functions. Main difference between those is tha ...
- Codeforces Round #350 (Div. 2) A B C D1 D2 水题【D2 【二分+枚举】好题】
A. Holidays 题意:一个星球 五天工作,两天休息.给你一个1e6的数字n,问你最少和最多休息几天.思路:我居然写成模拟题QAQ. #include<bits/stdc++.h> ...
- 002_UCOSIII任务创建于删除
(一)先创建一个启动任务来进行创建其它任务,创建任务的宏定义 #define START_TASK_PRIO 3 //任务优先级 #define START_STK_SIZE 128 //任务堆栈大小 ...
- jquer属性 offset、position、scrollTop
尺寸操作 1.获取宽高 a) jq对象.height/width () :只有获取高度/宽度 尺寸,不包括padding和margin 和 border 2.设置宽度 ...
- 探讨一下js中的继承和原型链
---恢复内容开始--- 每个JS对象一定对应一个原型对象,并从原型对象继承属性和方法. 也就是说 对象的__proto__属性的值就是它所对应的原型对象, 而prototype 只有函数才有的属性. ...
- spring事务在web环境中失效的问题
今天温习一下spring事务的时候,出现了一种诡异的现象,在java环境中测试事务是可以的.然后到web下测试事务就没用了.spring.xml配置 spring-mvc.xml配置 后来百度发现是因 ...