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)的更多相关文章

  1. leetcode第39题--Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  2. leetcode第38题--Combination Sum

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  3. 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 ...

  4. LeetCode(40) Combination Sum II

    题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

  5. LeetCode笔记:39. Combination Sum

    题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...

  6. LeetCode(39) Combination Sum

    题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...

  7. leetcode个人题解——#39 Combination Sum

    思路:先对数据进行排序(看评论给的测试数据好像都是有序数组了,但题目里没有给出这个条件),然后回溯加剪枝即可. class Solution { public: ; vector<vector& ...

  8. LeetCode:40. Combination Sum II(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...

  9. Leetcode 之 Combination Sum系列

    39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...

  10. 【leetcode刷题笔记】Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. ubuntu部署git

    先更新本机内置的程序. sudo apt-get updatesudo apt-get upgrade再判断系统是否内置了add-apt-repository命令,如果没有执行下列命令安装 sudo ...

  2. MQTT 单片机端讲解

    有空了和大家分享一下,如何从头架构一个高效mqtt并行客户端,基于传统GPRS等较差网络环境和网关等网络环境好的情景(当然仔细讲解mqtt的基本函数使很有必要的).---这会正忙着搬砖 MQTt协议 ...

  3. ul li 的 float:left;

    如 ul li{float:left;} 出来的效果不仅是原本默认竖着排的元素变横排,还是往左边排,重点是元素是按顺序排的,如果float等于right,则不仅是往右排,且元素是倒着排的,如原来的a ...

  4. python--列表、字典、元组、集合对比

    数据类型# 计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定义不同的数据类型.# a:整形和浮点型(正数和负数)# b:布尔类型(true,fals ...

  5. 函数调用的方法一共有 4 种,call,apply,bind

    1. 每个函数都包含两个非继承而来的方法:call()方法和apply()方法. 2. 相同点:这两个方法的作用是一样的. 都是在特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖 ...

  6. 理解HTTP协议(转载)

    一.HTTP协议的演进 HTTP(HyperText Transfer Protocol)协议是基于TCP的应用层协议,它不关心数据传输的细节,主要是用来规定客户端和服务端的数据传输格式,最初是用来向 ...

  7. 树和二叉树->基础知识

    树的表示方法 1 一般表示法 2 广义表表示法 3 凹入表示法 树的基本术语: 树:n(n>=0)个结点的有限集 结点:包含一个数据元素及若干指向其子树的分支 结点的度:结点拥有的子树数成为结点 ...

  8. jquery重置表单

    表单一般都有重置功能,在重置表单时需要将各个输入框中的值清空,如果输入框比较多,一个一个清空会比较麻烦,使用jquery的方法直接将表单中的所有输入框全部清空,首先给出一个form表单: <fo ...

  9. inet超级服务器和守护进程

    inetd是监视一些网络请求的守护进程,其根据网络请求来调用相应的服务进程来处理连接请求.它可以为多种服务管理连接,当 inetd接到连接时,它能够确定连接所需的程序,启动相应的进程,并把 socke ...

  10. discuz优化10个小技巧

    Discuz论坛是国内使用最多的论坛系统,现在最新版为X 3.4,X3.4 从 2018 年 1 月 1 日起只在官方 Git 发布,地址:https://gitee.com/ComsenzDiscu ...