给定一个数组 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)的更多相关文章

  1. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  2. LeetCode 40. 组合总和 II(Combination Sum II)

    题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...

  3. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  4. 40. 组合总和 II + 递归 + 回溯 + 记录路径

    40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...

  5. 40组合总和II

    题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...

  6. LeetCode 中级 - 组合总和II(105)

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  7. Leetcode题库——40.组合总和II

    @author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...

  8. 40. 组合总和 II leetcode JAVA

    题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使 ...

  9. leetcode 39. 组合总和(python)

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

随机推荐

  1. 列表and元组操作

    一.列表  列表是我们以后比较常用的数据类型之一,通过列表我们可以实现对数据的存储.修改等操作. 首先,我们看一下列表的定义: 有了列表以后,我们可以通过下标来访问列表中的元素.注意:下表是从0开始的 ...

  2. ELK报错及解决方案

    ELK报错及解决方案 1.jdk版本问题 报错如下: future versions of Elasticsearch will require Java 11; your Java version ...

  3. 连接数据库出现错误:1045-Access denied for user 'root'@'localhost'解决方法

    Navicat for MySQL 链接: https://pan.baidu.com/s/1slwQxVB 密码: r737 1.出现这个问题的原因之一是权限的问题,也就是说你的电脑可能没有权限访问 ...

  4. 一、ASP.NET Iframework_SignalR集线器类(v2)

    一.新建项目,选MVC项目默认 添加mvc文件夹和核心引用 二.添加SignaIR包 SignalR的准备:NuGet包管理器搜索:工具——>库程序包管理器——>Microsoft.Asp ...

  5. Python自动化学习--元素定位

    from selenium import webdriver import time driver = webdriver.Chrome() driver.get("https://www. ...

  6. BZOJ 2560: 串珠子 (状压DP+枚举子集补集+容斥)

    (Noip提高组及以下),有意者请联系Lydsy2012@163.com,仅限教师及家长用户. 2560: 串珠子 Time Limit: 10 Sec Memory Limit: 128 MB Su ...

  7. visual studio中配置opencv

    第1步附加包含目录:H:\software\programming\opencv\opencv\build\include 第2步附加库目录:H:\software\programming\openc ...

  8. 关于 const char *ptr,char const *ptr,char *const ptr 的讨论

    对于每个做C/C++的伙伴来说,面试中少不了关于const 的考察,尤其是对于刚毕业的新人. 今天听见同事在讨论这个问题,就随手写一下自己的理解.希望对大家又所帮助. 首先来说一下char *ptr: ...

  9. java集合类图详解

  10. java Character类源码分析

    一.使用 构建Character对象: public class CharTest { public static void main(String[] args) { Character c1 = ...