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. JSthis对象

    第一章: this是javascript语言的一个关键字,它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如 function test(){ ; } 随着函数使用场合的不同,this ...

  2. CentOS 7解决Local Time与实际时间相差8小时问题

    通过date -s “2014-12-06 15:00:00”以及timedatectl set-time “2014-12-06 15:00:00” ,以及ntp等方式均知识临时有效,苦恼了我半天. ...

  3. C++学习笔录1

    1.在实际开发中,引用类型变量值用于函数的参数中.它不会另外开辟空间(提高了程序效率),他相当于变量的别名,代表的就是当前这个变量的地址空间.(引用的底层用的是指针.因此从底层的角度讲,其实它的效率是 ...

  4. PHP获取中文汉字首字母方法

    function getFirstLetter($str){ $fchar = ord($str{0}); if($fchar >= ord("A") and $fchar ...

  5. 关于Centos Linux系统安装Python的问题

    由于最近在研究Python框架Django的使用,安装django扩展没有问题 新建项目  django-admin startproject projectName  如果什么都不修改或者直接创建一 ...

  6. Android 多状态按钮 ToggleButton

    ToggleButton      选中状态,未选中状态并且需要为不同的状态设置不同的显示文本.      属性:           checked="true"         ...

  7. how to install git 1.8 rpm

    git版本在低于1.8之前,对于私有项目会出现401的pull失败错误,只能通过升级git版本来解决 It appears that git18 is no longer available from ...

  8. SingleNumber python实现

    Single Number Given an array of integers, every element appears twice except for one. Find that sing ...

  9. J2SE知识点摘记(二十四)

     覆写hashCode() 在明白了HashMap具有哪些功能,以及实现原理后,了解如何写一个hashCode()方法就更有意义了.当然,在HashMap中存取一个键值对涉及到的另外一个方法为equa ...

  10. Oracle EBS-SQL (BOM-1):检查供应类型错误.sql

    --检查供应类型错误 SELECT MSI.SEGMENT1                物料编码, MSI.DESCRIPTION             物料描述, DECODE(MSI.WIP ...