leetcode 40. 组合总和 II (python)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
说明:
所有数字(包括目标数)都是正整数。
解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
if len(candidates)<=:
return []
candidates.sort()
self.res = []
self.dfs(candidates,[],target,)
return self.res
def dfs(self,candidates,sublist,target,index):
if target == :
self.res.append(sublist)
return
if target < :
return
for i in range(index,len(candidates)): # 从当前index开始遍历
if i != index and candidates[i] == candidates[i-]: # 保证每个数字只使用一次
continue
self.dfs(candidates,sublist+[candidates[i]],target-candidates[i],i+)
参考链接: https://blog.csdn.net/weixin_40546602/article/details/88357837
回溯递归函数dfs(self,candidates,sublist,target,index),index为当前遍历到数组的位置,因为不允许重复使用元素,只可依次遍历。
剪枝操作1: 当target值已经小于0,由于数组中包含负数,所有之间返回
剪枝操作2:保证每个数字只使用一次
leetcode 40. 组合总和 II (python)的更多相关文章
- Java实现 LeetCode 40 组合总和 II(二)
40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...
- LeetCode 40. 组合总和 II(Combination Sum II)
题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- 40. 组合总和 II + 递归 + 回溯 + 记录路径
40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...
- 40组合总和II
题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...
- LeetCode 中级 - 组合总和II(105)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- Leetcode题库——40.组合总和II
@author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...
- 40. 组合总和 II leetcode JAVA
题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使 ...
- leetcode 39. 组合总和(python)
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
随机推荐
- docker容器配置hosts
在mac开发的时候,docker容器没有配置hosts,但是mac本机配置了hosts,这个本机的hosts配置对docker容器里面的所有容器都适用,但是到了linux的时候反而不适用了 可以通过下 ...
- go & RabbitMQ
参考 RabbitMQ tutorial - 官方示例 Go code for RabbitMQ tutorials - 官方示例源码 go语言开发RabbitMQ-牛刀小小试试
- numpy.random.randn()和numpy.random.rand()
1 numpy.random.rand() (1)numpy.random.rand(d0,d1,…,dn) rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1 dn表格每个维度 返回 ...
- react 错误处理
https://www.jianshu.com/p/61d09e488743 https://codepen.io/sgroff04/pen/dVbgJy/
- 使用pdfjs插件在线预览PDF文件
前言 本文介绍在html中使用 pdfjs插件在线预览PDF文件的方法. 实现步骤 下载 pdfjs 并引入项目中 到PDFJS官网 http://mozilla.github.io/pdf.js/g ...
- php设置错误级别
ini_set('display_errors', 1); error_reporting(E_ALL);
- Redis5新特性
Redis5.0的12个新特性 1.数据类型Stream 本质上是一个消费者等待生产者发送新的数据 使用情景 其他五种数据结构无法实现的需求,可以通过stream来实现 直接贴近业务需求,提升开发效率 ...
- Linux系统性能测试工具(五)——磁盘io性能工具之fio
本文介绍关于Linux系统(适用于centos/ubuntu等)的磁盘io性能测试工具-fio.磁盘io性能测试工具包括: fio: dd
- mirror - 映射在远端节点上的档案
总览 SYNOPSIS mirror [flags] -gsite:pathname mirror [flags] [package-files] 描述 DESCRIPTION Mirror 是以 P ...
- thinkphp5 select对象怎么转数组?
DB操作返回是数组.模型直接操作返回是对象 对象类型转换数组打开 database.php 增加或修改参数'resultset_type' => '\think\Collection',即可连贯 ...