leetcode第40题:组合总和II
给定一个数组 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]
] 解题思路类似上一题,
不同之处是这道题不允许重复使用candidates中的元素。
我们可以直接在上一道题目的代码上修改,递归的时候将 idx 加 1(需判断是否超出candidates的范围),另外由于题目输入的candidates可能包含相同的元素,所以我们需要对得到的答案进行去重处理。 代码如下:
class Solution:
def Slover(self, candidates, target, res, path, idx):
for i in range(idx, len(candidates)):
new_target = target - candidates[i]
if new_target < 0:
return
else:
if new_target == 0:
res.append(path + [candidates[i]])
else:
idx = idx + 1
if idx < len(candidates):
self.Slover(candidates, new_target, res, path + [candidates[i]], idx)
else:
return def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = []
path = []
idx = 0
candidates = sorted(candidates)
self.Slover(candidates, target, res, path, idx)
ud_res = []
for r in res:
if r not in ud_res:
ud_res.append(r)
return ud_res
leetcode第40题:组合总和II的更多相关文章
- 【JavaScript】Leetcode每日一题-组合总和4
[JavaScript]Leetcode每日一题-组合总和4 [题目描述] 给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target .请你从 nums 中找出并返回总和为 targ ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- Java实现 LeetCode 40 组合总和 II(二)
40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...
- 40. 组合总和 II + 递归 + 回溯 + 记录路径
40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...
- 【python】Leetcode每日一题-反转链表 II
[python]Leetcode每日一题-反转链表 II [题目描述] 给你单链表的头节点 head 和两个整数 left 和 right ,其中 left <= right .请你反转从位置 ...
- 组合总和 II
组合总和 II 题目介绍 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...
- 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 ...
随机推荐
- php字符串 统计个数
方法一 $arr=str_split($str); $arr=array_count_values($arr); /* * 方法二 * */ $arr = str_split($str); $a2 = ...
- Tempter of the Bone HDU - 1010
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...
- SWUST OJ (943)
顺序表插入操作的实现 #include<stdio.h> #include <stdlib.h> void InitList(int *&l, int n) { l = ...
- tcpcopy真实流量压测工具
https://quentinxxz.iteye.com/blog/2249799 http://blog.chinaunix.net/uid-25057421-id-5576741.html htt ...
- ZCRM_DAY_IN_WEEK
FUNCTION zcrm_day_in_week. *"------------------------------------------------------------------ ...
- maven生命周期绑定要点
生命周期不执行任何操作,都是抱插件大腿 maven-core-3.3.9-sources.jar下META-INF/plexus/components.xml的定义了三个生命周期的插件绑定 参考:ht ...
- Oracle12c中配置实例参数和修改容器数据库(CDB)及可插拔数据库(PDB)
Oracle12c中的多宿主选项允许一个容器数据库(CDB)容纳多个独立的可插拔数据库(PDB).本文将展示如何配置实例参数和修改容器数据库(CDB)及可插拔数据库(PDB).1. 配置CDB中的实例 ...
- PhpDocumentor 生成文档
最近项目需要phpdoc生成文档,首先安装PhpDocumentor,利用pear安装: 切换用户: su root 安装PhpDocumentor: pear install PhpDocument ...
- XAMPP/PHPnow/phpStudy安装使用对比
一.XAMPP 1.1 下载 下载地址:https://www.apachefriends.org/download.html 1.2 安装 双击下载的安装程序进行安装,界面如下图,都是典型的下一步下 ...
- Ubuntu 16 修改时区!
网上大部分解决办法是命令tzselect,然后选择亚洲-->中国->上海,但很遗憾,一点效果没有:后找到解决办法,运行命令dpkg-reconfigure tzdata,选择Asia--& ...