【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 ...
随机推荐
- 分布式传输协调程序MSDTC配置
尊重原著作:本文转载自http://blog.csdn.net/wilsonke/article/details/8468438 配置说明文件:http://files.cnblogs.com/hlx ...
- Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://127.0.0.1:3306/test'
原来的配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...
- jQuery中的类型判断
在JQuery中有一个type方法,在1.11.2中是这样写的 var class2type = {}; var toString = class2type.toString; jQuery.each ...
- this指针与function变量--this究竟指向哪里?
参考文章:<深入浅出 JavaScript 中的 this> http://www.ibm.com/developerworks/cn/web/1207_wangqf_jsthis/ Ja ...
- Vnstat: 简单实用的网络流量统计工具
官方主页: http://humdi.net/vnstat # Ubuntu 安装: (其本上其它发行版的包管理程序中也都包含了这款软件,请自行安装) sudo apt-get install vns ...
- 汇编写下strcpy
#include <stdio.h> int main() { char *source = "hello world\n"; ] = {}; char *p = de ...
- C# Socket SSL通讯笔记
一.x.509证书 1.制作证书 先进入到vs2005的命令行状态,即:开始-->程序-->Microsoft Visual Studio 2005-->Visual Studio ...
- Linux常用命令--网络管理篇(三)
ping –b 10.0.0.255 扫描子网网段 ifconfig 查看网络信息 netconfig 配置网络,配置网络后用service network restart重新启动网络 ifconfi ...
- VMware ESXI4.1 常用命令
一. VMware ESX Command 1. # vmware –v (查看esx版本) 2. # esxcfg-info -a(查看显示ESX硬件,内核,存储,网络等信息) # esxcfg- ...
- Oracle中强行断开用户连接的方法
版权声明:本文为博主原创文章,未经博主允许不得转载. 首先查找目标用户的当前进程,注意是serial#而不是serial,网上有的介绍漏掉了#: select sid,serial# from v$s ...