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.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • 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, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6]

题意分析:

  本题是给定一个数字集合(可能有重复)和一个目标值,让你找出所有可能的组合,使之和为目标值。所有的数字只能使用一次,要求得到的组合不能有重复,并且组合里面的数字必须是升序。

解答:

  对比一下【LeetCode题意分析&解答】39. Combination Sum,唯一的区别在于本题数字只能使用一次,而不是无限次。所以我们只需要对上一题的代码稍作修改,就可以实现本题的要求。

AC代码:

class Solution(object):
def combinationSum2(self, candidates, target):
ret_list = []
def backtracing(target, candidates, index, temp_list):
if target == 0:
ret_list.append(temp_list)
elif target > 0:
for i in xrange(index, len(candidates)):
# remove duplicates
if i != index and candidates[i] == candidates[i - 1]:
continue
backtracing(target - candidates[i], candidates, i + 1, temp_list + [candidates[i]]) backtracing(target, sorted(candidates), 0, [])
return ret_list

【LeetCode题意分析&解答】40. Combination Sum II的更多相关文章

  1. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

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

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

  3. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  4. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. 【LeetCode】40. Combination Sum II (2 solutions)

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  7. 【LeetCode题意分析&解答】39. Combination Sum

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

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

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

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

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

随机推荐

  1. OC中文件的操作

    OC中文件操作,在之前的文章中,已经接触到了文件的创建了,但是那不是很具体和详细,这篇文章我们就来仔细看一下OC中是如何操作文件的: 第一.首先来看一下本身NSString类给我们提供了哪些可以操作文 ...

  2. JS 精粹(一)

    先说说JS是容易被误解的语言的观点.从名字上来看,"Java"这似乎暗示着这门语言与Java的关系:好像这门语言是Java的子集,或比Java弱.但实际与Java并无关系,如果说非 ...

  3. C++学习笔录2

    1.如果一个类要成为基类,那么它的成员变量声明成受保护的变量,既用关键字protected修饰. 2.处理共同继承产生的二义性:采用虚继承方式,当出现两个相同的成员时,编译器会自动删除其中一个.其方法 ...

  4. PHP基础示例:简单的在线文件管理

    先截个图: 下面为代码部分,由于只有一个文件,所以就不折叠了. <?php //简单的在线文件管理 $path = "./"; $filelist=array("f ...

  5. 精读《javascript高级程序设计》笔记二——变量、作用域、内存以及引用类型

    变量.作用域和内存问题 执行环境共有两种类型——全局和局部 作用域链会加长,有两种情况:try-catch语句的catch块,with语句. javascript没有块级作用域,即在if,for循环中 ...

  6. Android Studio 添加Assets目录

    Android Studio 添加Assets目录: 法一: Since Android Studio uses the new Gradle-based build system, you shou ...

  7. css3之border-color

    -moz-border-top-colors:上边框-moz-border-right-colors:右边框-moz-border-bottom-colors:下边框-moz-border-left- ...

  8. Java编程中提高性能的几点建议

    尽量减少对变量的重复计算 如 for(int i=0;i<list.size();i++) 应该改为 for(int i=0,len=list.size();i<len;i++) 并且在循 ...

  9. 软件测试学习日志———— round 2 Junit+intellj idea 安装及简单的测试使用

    今天是软件测试的上机,主要内容是对junit的安装以及对一个简单类的测试实践.老师推荐用eclipse,但是我原来一直在 用intellj Idea,所以我试了试intellj Idea对junit的 ...

  10. mysql错误提示不是英语的解决办法

    mysql提示突然就变成法语了,google了一下,找到了解决方法:打开my.ini文件,找到[mysqld]配置项如下 [mysqld] port explicit_defaults_for_tim ...