[LeetCode]题解(python):039-Combination Sum

题目来源:

  https://leetcode.com/problems/combination-sum/



题意分析:

  输入一个set和一个target,找出所以由set里面的数组成的相加等于target的组合。组合必须按照字典序排序。



题目思路:

  由于组合必须按照字典序排序。那么首先将set排序。不难发现,题目可以分成两种情况,第一个组合不包括set[0],这种情况就去掉set[0];另外一种是包括set[0],这种情况就是将target - set[0]。用递归来解决这个问题即可。



代码(python):

  

 1 class Solution(object):
 2     def boolcombinationSum(self, candidates, target,j):
 3         ans = [];size = len(candidates)
 4         if target == 0:
 5             return []
 6         if size < j + 1 or target < 0:
 7             return [[-1]]
 8         tmp1 = self.boolcombinationSum(candidates,target,j + 1);tmp2 = self.boolcombinationSum(candidates,target - candidates[j],j)
 9         if len(tmp2) == 0:
10             ans.append([candidates[j]])
11         elif tmp2 != [[-1]]:
12             for i in range(len(tmp2)):
13                 ans.append([candidates[j]] + tmp2[i])
14         if len(tmp1) != 0 and tmp1 != [[-1]]:
15             for i in range(len(tmp1)):
16                 ans.append(tmp1[i])
17         if len(tmp2) != 0 and tmp1 == [[-1]] and tmp2 == [[-1]]:
18             return [[-1]]
19         return ans
20     def combinationSum(self, candidates, target):
21         """
22         :type candidates: List[int]
23         :type target: int
24         :rtype: List[List[int]]
25         """
26         candidates.sort()
27         ans = self.boolcombinationSum(candidates,target,0)
28         if ans == [[-1]]:
29             return []
30         return ans



转载请注明出处:http://www.cnblogs.com/chruny/p/4926306.html

时间: 2024-10-29 19:11:51

[LeetCode]题解(python):039-Combination Sum的相关文章

[LeetCode] 039. Combination Sum (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 039. Combination Sum (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给出一些正整数集合,以及一个目标数,从集合中选择一

039 Combination Sum

039 Combination Sum 这道题就是暴力搜索, 但是有个优化 就是 Line 14 处加一个 if a[s] > target 就不需要重复搜索 这样运行时间会从 236ms 变成108ms 1 class Solution: 2 # @param {integer[]} candidates 3 # @param {integer} target 4 # @return {integer[][]} 5 def combinationSum(self, candidates, ta

[Leetcode][Python]40: Combination Sum II

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 40: Combination Sum IIhttps://oj.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 candi

LeetCode 039 Combination Sum

题目要求:Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (in

Leetcode 39 40 216 Combination Sum I II III

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

【LeetCode】039. Combination Sum

题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers

[Leetcode][Python]39: Combination Sum

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 39: Combination Sumhttps://oj.leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a target number (T),find all unique combinations in C where the candidate numbers

Java for LeetCode 039 Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will

leetcode第38题--Combination Sum

题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) w

leetcode第39题--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