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

Each number in C 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.

For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:

  1. [
  2. [1, 7],
  3. [1, 2, 5],
  4. [2, 6],
  5. [1, 1, 6]
  6. ]
 

这道题和Leetcode 39差不多,区别是一个元素只能用一次,所以L27里面用了candidates[i+1:]。另外需要注意的是,[1a,2,5] 和 [1b,2,5]在结果中应被视为重复解而去掉一个。所以用了L13-L16。

  1. class Solution(object):
  2. def combinationSum2(self, candidates, target):
  3. """
  4. :type candidates: List[int]
  5. :type target: int
  6. :rtype: List[List[int]]
  7. """
  8. candidates.sort()
  9.  
  10. res = []
  11. self.helper(candidates, target, res, [])
  12.  
  13. ans = []
  14. for elem in res:
  15. if elem not in ans:
  16. ans.append(elem)
  17.  
  18. return ans
  19.  
  20. def helper(self, candidates, target, res, line):
  21. if target == 0:
  22. res.append([x for x in line])
  23.  
  24. for i,x in enumerate(candidates):
  25. if x <= target:
  26. line.append(x)
  27. self.helper(candidates[i+1:], target - x, res, line)
  28. line.pop()

Leetcode 40. Combination Sum II的更多相关文章

  1. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  2. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  3. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  4. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. LeetCode 40. Combination Sum II (组合的和之二)

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

  6. leetcode 40 Combination Sum II --- java

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

  7. [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...

  8. Java [Leetcode 40]Combination Sum II

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

  9. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

随机推荐

  1. 解决PKIX:unable to find valid certification path to requested target 的问题

    这两天在twitter服务器上忽然遇到这样的异常: e: sun.security.validator.ValidatorException: PKIX path building failed: s ...

  2. rails程序文件名命名规范

    1 一般文件名是用小写单词加下划线分割,但类的名字用骆驼法.例如 sessions_controller.rb中定义SessionsController. 2 helpers内的文件为辅助类,定义了许 ...

  3. Ruby的模型关系随笔

    1 Class和Module的实例方法也就是所有具体类和具体Module的类方法,因为具体类和具体Module分别是Class和Module的实例.例如Object.new对应着Class#new,K ...

  4. web.xml中监听器配置

    <!-- 监听器的配置:监听器配置完以后,应用系统在启动的时候就会开启这些监听器. 监听器的理解:监听器好比一个卫兵,卫兵一直站在那里等待长官的命令,当卫兵收到长官的命令以后,立即执行 之前已经 ...

  5. Oracle 释放flash recovery area的四种方法

    早上收到一台Linux服务器磁盘告警邮件以及监控告警日志程序发来的邮件.检查过后,发现Linux服务器中一个分区没有空间了.主要原因是由于昨晚程序员做升级时,产生了大量的归档日志,导致联机重做日志无法 ...

  6. ORA-01157 & ORA-01110

    测试服务器做了RMAN还原后,发现告警日志文件有如下错误信息ORA-01110: data file 206: '/u04/epps/oradata/temp02.dbf'   Errors in f ...

  7. Ignite 配置更新Oracle JDBC Drive

           如果使用Oracle 12C 作为Ignite 的Repository的话,在Repository Createion Wizard的配置过程中,会出现ORA-28040:No matc ...

  8. jQuery validator自定义

    项目中接触到validator,记录下 jQuery.validator.addMethod("isStrongPwd", function(value, element){ va ...

  9. android Intent使用

    ntent.setType(“image/*”);//图片格式 intent.setType(“audio/*”); //选择音频 intent.setType(“video/*”); //选择视频 ...

  10. Nginx中的一些匹配顺序

    Nginx中经常需要做各种配置,总结如下: 1.server_name配置 nginx中的server_name指令主要用于配置基于名称虚拟主机,同一个Nginx虚拟主机中,可以绑定多个server_ ...