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 ...
随机推荐
- mysql基础篇--修改
语法 #修改单表记录 update 表名 set 列=新值,列=新值,... where 筛选条件; #修改多表记录 update 表1 别名 inner|left|right join 表2 别名 ...
- STL练习板子题(c++11警告)
第一题 词典 总时间限制: 3000ms 内存限制: 65536kB 描述 你旅游到了一个国外的城市.那里的人们说的外国语言你不能理解.不过幸运的是,你有一本词典可以帮助你. 输入 首先输入一个词典, ...
- Vue 组件中锚点定位的问题
1 当前组件的顶部 this.$el.scrollIntoView() 2 指定的 Element this.$el.querySelector(selector).scrollIntoView() ...
- 安裝PHPBB
1.下載PHPBB https://www.phpbb.com/downloads/ 2下載PHP http://windows.php.net/download/ 很多教程都介紹在WIN7用ISAP ...
- 编写测试类实现并发访问固定URL(亲测能用!!!)
1.类目录 2.LatchTest.java类 package com.test; import java.util.concurrent.CountDownLatch; public class L ...
- ActiveMQ C#实例
重点参考:NMS Documentation 一.ActiveMQ Queue 在ActiveMQ中Queue是一种点对点的消息分发方式,生产者在队列中添加一条消息,然后消费者消费一条消息,这条消息保 ...
- Noip2001 提高组 T3
T3 题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k<=40),且每份中包 ...
- element-ui遮罩层el-dialog的使用
template <el-button type="text" @click="dialogVisible = true">点击打开 Dialog& ...
- 常使用的VIM命令及文件颜色代表含义
编辑模式--->输入模式 i : insert 在光标所在处输入: a:append 在光标所在处后面输入: o:在当前光标所在行的下方打开一个新行: I:在当前光标所在行的行首输入: A:在当 ...
- kubernetes部署nginx/tomcat
kubernetes集群已经部署好了,需要的话可以参考之前的文章https://www.cnblogs.com/winter1519/p/10015420.html [root@master tomc ...