题目来源


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. python 代码片段6

    #coding=utf-8 # python常用的列表list和字符串string # tuple元组,一个身有残疾的只读列表 s='python' print s[0] print s[-1] # ...

  2. BZOJ2725 : [Violet 6]故乡的梦

    如果S==T,那么答案为0. 如果S与T不连通,那么答案为inf. 否则,S到T的最短路径上至少有一条边. 求出以S为源点的最短路图,是个DAG,随便抓一条S到T的最短路,记为P. 设dpS[x]表示 ...

  3. 【原】storm源码之理解Storm中Worker、Executor、Task关系

    Storm在集群上运行一个Topology时,主要通过以下3个实体来完成Topology的执行工作:1. Worker(进程)2. Executor(线程)3. Task 下图简要描述了这3者之间的关 ...

  4. CentOS6.4 内核优化

    vi /etc/sysctl.conf net.ipv4.tcp_syncookies = net.ipv4.tcp_tw_reuse = net.ipv4.tcp_tw_recycle = net. ...

  5. [leetCode][001] Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  6. BestCoder Round #74

    身败名裂啊...... T1WA了半天,30min才A. T2又WA了一发,然后Hack刚2min就被别人叉了. T3做完后最后40min不知所措. 去叉别人,看到一个人写D题判m=0很奇怪,随手把他 ...

  7. Graph database_neo4j 底层存储结构分析(8)

    3.8  示例1:neo4j_exam 下面看一个简单的例子,然后看一下几个主要的存储文件,有助于理解<3–neo4j存储结构>描述的neo4j 的存储格式. 3.8.1    neo4j ...

  8. ASP.NET WEB API的服务托管(Self-HOST)

    如果我们想对外发布RESTful API,可以基于ASP.NET来构建Restful APIs,但需要部署IIS吗?答案是不必.你可以把它托管到一个Windows Service.具体如何把WEB A ...

  9. Leetcode | N-Queens I & II

    N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...

  10. 前端网址收集!Amazing! 神奇!

    (1)Bootstrap中文网 http://www.bootcss.com/ (2)前端编码规范 http://codeguide.bootcss.com/ (3)bootstrap可视化布局+下载 ...