【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 ...
随机推荐
- Android摸索一环境搭建
用户场景:做语音保全,首先要拨打400号码,听到语音后需要先按1然后再拨打对方号码,那么问题就来了,如果我记不清楚对方号码!是不是很苦逼----我要先找到对方号码然后用笔或者其他方式把这个号码记录下来 ...
- 二进制安装mysql
1.1 MySQL安装介绍 mysql软件下载地址信息: www.mysql.com mirrors.sohu.com mysql软件下载完毕后,查看mysql解压后目录文件大小 1.下载解压my ...
- 安装指定版本的nodejs
node有一个模块n,是专门用来管理node.js的版本的. 1.安装n模块: npm install -g n 2.升级node.js到最新稳定版 n stable 3.安装指定版本: n v6.1 ...
- deepin中idea中文乱码解决
打开终端 命令: sudo su 输入密码: aptitude search uming 显示: root@terwer-PC:/home/terwer# aptitude search uming ...
- PHP之fopen wrappers模块
一.fopen wrappers模块的配置 ①.allow_url_fopen boolean //出于安全性考虑,此选项只能在 php.ini 中设置 //本选项激活了 URL 形式的 fopen ...
- Web开发——JavaScript基础
参考学习: MDN JavaScript:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript ECMAScript 6入门(阮一峰):htt ...
- XML文件处理
XML XML简介 XML被设计用来传输和存储数据. XML是可扩展标记语言. 解析XML的几种方式 DOM,DOM4j------先把XML文件读取到内存中,然后再解析 SAX----------边 ...
- Androidstudio_LinearLayout
- 《Linux 文本处理》- awk 分析 nginx 日志
废话不多说,简单了解一下 awk - 强大的文本分析工具,也就是分析日志 最常用的就是分析日志了吧,做统计什么,这里也拿 nginx 日志来做分析 1:统计出现次数 - 比如状态码出现次数 - 先直接 ...
- Win10系统桌面右键新建没有内容怎么恢复
Win10系统桌面右键新建没有内容怎么恢复 添加txt文本文档 运行->regedit,打开注册表编辑器 展开HKEY_CLASSES_ROOT 找到.txt 选中.txt,修改右侧窗格的“默认 ...