【LeetCode题意分析&解答】40. Combination Sum II
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的更多相关文章
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- 【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 ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- [转]Visual Studio 2008中如何比较二个数据库的架构【Schema】和数据【Data】并同步
使用场景: 在团队开发中,每一个人都有可能随时更新数据库,这时候数据库中数据和架构等信息都会发生变化.如果更新不及时,就会发生数据错误或数据丢失的风险,影响团队的开发效率和 项目进度,这时候我们该怎么 ...
- Oracle的总体回顾
1.多表查询:一张以上的表进行查询,称为多表查询,多表查询的时候可以为表指定别名的方式以简化查询列的编写,在多表查询中,会产生笛卡尔积,就是两张表的总数相乘得到的结果,如果要想消除笛卡尔积要通过关联条 ...
- HTTP头信息(转)--2
HTTP 头部解释 ========================================================================================== ...
- 关于ThreadAbortExcption异常处理
之前程序中,使用Thread.Abort()方法来终止线程的运行,但它是抛出ThreadAbortException异常来终止线程. 异常信息摘要: Unhandled Exception:Threa ...
- PHP EOF(heredoc)的使用
<?php /* Heredoc技术,在PHP手册和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术. 目前一些论坛程序和CMS系统使用了这种技术,前不久看一个朋友的P ...
- asp.net mvc 注册中的邮箱激活功能实现(一)
基本流程图 注册页面就不再写出,现在将发送邮件的代码粘贴出来 public ActionResult SendEmial() { ; string validataCode = System.Guid ...
- jni note
2016-1-15 javah 使用javah可以自动从java文件生成jni头文件, 用法:javah [选项] <类> 其中 [选项] 包括: -help ...
- 关于Linux Kernel 2.6.28 以上有缺陷,在第208.5天自行重啟的问题
今天看到一转帖如下: Linux Kernel 2.6.28 以上有缺陷,在第208.5天自行重啟 https://access.redhat.com/knowledge/solutions/ ...
- header.htm
<!--{ad/subnavbanner/a_mu}--> 的意思是 全局 页头二级导航栏广告 位 <!--{subtemplate common/pubsearchform}- ...
- 在非gui线程使用QMessageBox
最近我写项目的时候遇到一个奇怪的需求,要在工作线程内,根据某个情况弹出一个MessageBox 但是Qt提供的MessageBox只可以在gui线程(主线程)使用,于是我就对QMessageBox封装 ...