【LeetCode每天一题】Combination Sum II(组合和II)
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.Each number in candidates may only be used once in the combination.
Note:
- All numbers (including
target) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
Input: candidates =[10,1,2,7,6,1,5], target =8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]
思路
这道题的思路和前一道组合数的思路都是类似的,只不过在细节处理上不同。详细思路见代码
解决代码
class Solution(object):
def combinationSum2(self, nums, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
if len(nums) < 1: # nums为空直接返回
return []
nums.sort() # 对nums进行排序,主要目的是为了较少递归次数。
res =[]
self.get_res(nums, target, [], res) # 递归
return res def get_res(self, nums, target, path, res):
if target == 0: # 结束条件,找到满足的组合将其添加进res列表中
res.append(path)
return
if target < 0: # 没有满足的组合直接返回
return
for i in range(len(nums)): # 从第下标0开始寻找
if i > 0 and nums[i] == nums[i-1]: # 如果当前下小标元素和前一个相等直接跳过,避免产生相同的组合。
continue
if nums[i] > target: # 当前下标直接大于target,后面的元素都不会满足直接返回。
break
self.get_res(nums[i+1:], target-nums[i], path+[nums[i]], res) # 下一次递归时,都将nums大小减一。意味着从下一个开始重新寻找满足条件的组合
【LeetCode每天一题】Combination Sum II(组合和II)的更多相关文章
- leetcode第39题--Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- leetcode第38题--Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- LeetCode笔记:39. Combination Sum
题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...
- LeetCode(39) Combination Sum
题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...
- leetcode个人题解——#39 Combination Sum
思路:先对数据进行排序(看评论给的测试数据好像都是有序数组了,但题目里没有给出这个条件),然后回溯加剪枝即可. class Solution { public: ; vector<vector& ...
- LeetCode:40. Combination Sum II(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...
- Leetcode 之 Combination Sum系列
39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- C++判断是否连接服务器
BOOL CheckServerStatus::isConnectServer(CString serverName, int serverPort) { CString strURL; strURL ...
- TOP100summit:【分享实录-美团点评】 业务快速升级发展背后的系统架构演进
本篇文章内容来自2016年TOP100summit美团●大众点评高级技术专家,酒店后台研发组eHome团队负责人许关飞的案例分享.编辑:Cynthia 许关飞:美团●大众点评高级技术专家,酒店后台研发 ...
- win10屏幕变灰怎么解决?
朋友们在使用电脑过程中最高频使用的快捷键可能就是ctrl+c, ctrl+v了,但是殊不知,有时候按的太快产生误触,触发了ctrl+win+c,是屏幕变成了灰色,只需要再次同时按下这三个键就可以恢复彩 ...
- tensorflow如何正确加载预训练词向量
使用预训练词向量和随机初始化词向量的差异还是挺大的,现在说一说我使用预训练词向量的流程. 一.构建本语料的词汇表,作为我的基础词汇 二.遍历该词汇表,从预训练词向量中提取出该词对应的词向量 三.初始化 ...
- Oracle的一些经典SQL面试题
实例1:测试数据: create table nba( team varchar2(20), year number(4) ) SQL> select * from nba; TEAM ...
- dbclient python ---influxdb -install -relay--http write--read.[create db]
1s=1000ms 1ms=1000 microseconds 1microsecond=1000 nanoseconds+01:00 from influxdb import InfluxDBCli ...
- RHEL6.2 ORACLE11G
今天翻文件看到这个系统的安装截图才发现自己没写,补上. 惯例还是用到的所有参数均位于文末附录 启动虚拟机进入系统安装 选择跳过硬盘检测 选择语言 选择基本存储设备 设定主机名 设定口令 选择使用所有空 ...
- 20165336 实验一 Java开发环境的熟悉
20165336 实验一 Java开发环境的熟悉 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:康志强 学号:20165336 指导教师:娄嘉鹏 实验日期:2018年4月2日 实验 ...
- P1896 [SCOI2005]互不侵犯 状压dp
正解:状压dp 解题报告: 看到是四川省选的时候我心里慌得一批TT然后看到难度之后放下心来觉得大概没有那么难 事实证明我还是too young too simple了QAQ难到爆炸TT我本来还想刚一道 ...
- 【Python基础】*args,**args的详细用法
Python基础知识:*args,**args的详细用法 参考:https://blog.csdn.net/qq_29287973/article/details/78040291 *args 不定 ...