题目来源


https://leetcode.com/problems/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.


题意分析


Input: a list as candidates, a value named target

Output:the list number that sumed to target

Conditions:在list里面找若干个数,使得和为target,注意每个数可以取一次

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]


题目思路


与上题类似,先对list进行排序,然后穷举即可,注意可能会出现重复情况,所以需要去重(加一个判断语句),另外注意传递时的参数的范围


AC代码(Python)

 1 _author_ = "YE"
2 # -*- coding:utf-8 -*-
3
4 class Solution(object):
5 def find(self,candidates, target, start, valueList):
6 if target == 0:
7 if valueList not in Solution.ans:
8 Solution.ans.append(valueList)
9 length = len(candidates)
10 for i in range(start, length):
11 if candidates[i] > target:
12 return
13 self.find(candidates, target - candidates[i], i + 1, valueList + [candidates[i]])
14
15 def combinationSum2(self, candidates, target):
16 """
17 :type candidates: List[int]
18 :type target: int
19 :rtype: List[List[int]]
20 """
21 candidates.sort()
22 Solution.ans = []
23 self.find(candidates, target, 0, [])
24 return Solution.ans
25
26 candidates = [10,1,2,7,6,1,5]
27 target = 8
28 s = Solution()
29 print(s.combinationSum2(candidates,target))

[LeetCode]题解(python):040-Combination Sum II的更多相关文章

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

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

  2. LeetCode 040 Combination Sum II

    题目要求:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find al ...

  3. LeetCode(40) Combination Sum II

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

  4. 【LeetCode】040. Combination Sum II

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

  5. Java for LeetCode 040 Combination Sum II

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

  6. leetcode第39题--Combination Sum II

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

  7. 040 Combination Sum II 组合总和 II

    给定候选号码数组 (C) 和目标总和数 (T),找出 C 中候选号码总和为 T 的所有唯一组合.C 中的每个数字只能在组合中使用一次.注意:    所有数字(包括目标)都是正整数.    解决方案集不 ...

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

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

  9. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  10. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

随机推荐

  1. Color a Tree[HDU1055]

    Color a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  2. TYVJ P1013 找啊找啊找GF Label:动态规划

    做题记录:2016-08-15 22:19:04 背景 MM七夕模拟赛 描述 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手,你是我的好GF.再见.""诶,别再见啊.. ...

  3. POJ 3373 Changing Digits(DP)

    题目链接 记录路径的DP,看的别人的思路.自己写的也不好,时间居然2000+,中间的取余可以打个表,优化一下. 写的各种错,导致wa很多次,写了一下午,自己构造数据,终于发现了最后一个bug. dp[ ...

  4. linux-centos下源代码安装subversion (svn)

    1.svn的源代码 1.1 可以在官方下载,官方地址 :svn 1.6.17源码包  http://subversion.tigris.org/servlets/ProjectDocumentList ...

  5. js for循环,为什么一定要加var定义i变量

    我知道,有些人(譬如之前的我)写js的for循环时,都不习惯加上var,这当然是语法允许的.譬如下面. for(i=0;i<10;i++){//就不写成: var i=0 alert(i); } ...

  6. 李洪强 - C语言8-Scanf函数

    C语言的scanf函数 一.变量的内存分析 (一)字节与地址 ①. 内存以字节为单位 每个字节都有自己的内存地址,根据地址就可以找到该字节.整个内存相当于一整个酒店,而酒店以房间为单位,在这里每个房间 ...

  7. lucene 3.0.2 基本操作入门

    转自:Bannings http://blog.csdn.net/zhangao0086/article/details/ 我们为什么需要Lucene? 任何的的查询功能都类似,都是对文本内容的搜索, ...

  8. Redis 笔记与总结1 安装部署

    NoSQL 使用场景: 1.对数据高并发读写 2.对海量数据的高效率存储和访问 3.对数据的高可扩展性和高可用性 Redis 通常被称为数据结构服务器,因为键可以包含字符串(strings).哈希(h ...

  9. mysql null值的特殊处理

    实例 尝试以下实例: root@host# mysql -u root -p password; Enter password:******* mysql> use RUNOOB; Databa ...

  10. PHP面向对象程序设计的61条黄金法则

    PHP面向对象程序设计的61条黄金法则   你不必严格遵守这些原则,违背它们也不会被处以宗教刑罚.但你应当把这些原则看成警铃,若违背了其中的一条,那么警铃就会响起 . ----- Arthur J.R ...