[leetcode]Combination Sum @ Python

原题地址:https://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 sums
to T.

The same repeated number may be chosen
from C unlimited number of times.

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 2,3,6,7 and
target 7
A solution set
is: 
[7] 
[2, 2, 3]

解题思路:穷举出符合条件的组合,我们一般考虑dfs。

代码:


class Solution:
# @param candidates, a list of integers
# @param target, integer
# @return a list of lists of integers
def DFS(self, candidates, target, start, valuelist):
length = len(candidates)
if target == 0:
return Solution.ret.append(valuelist)
for i in range(start, length):
if target < candidates[i]:
return
self.DFS(candidates, target - candidates[i], i, valuelist + [candidates[i]])

def combinationSum(self, candidates, target):
candidates.sort()
Solution.ret = []
self.DFS(candidates, target, 0, [])
return Solution.ret

[leetcode]Combination Sum @ Python,布布扣,bubuko.com

时间: 2024-10-03 15:01:30

[leetcode]Combination Sum @ Python的相关文章

LeetCode: Combination Sum [038]

[题目] 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)

LeetCode: Combination Sum II [039]

[题目] 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

Leetcode: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 (includ

[leetcode]Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/path-sum/ 题意: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum =

Leetcode | Combination Sum I &amp;&amp; II

Combination Sum I 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 (includ

[leetcode]Two Sum @ Python

原题地址:http://oj.leetcode.com/problems/two-sum/ 题意:找出数组numbers中的两个数,它们的和为给定的一个数target,并返回这两个数的索引,注意这里的索引不是数组下标,而是数组下标加1.比如numbers={2,7,11,17}; target=9.那么返回一个元组(1,2).这道题不需要去重,对于每一个target输入,只有一组解,索引要按照大小顺序排列. 解题思路:1,由于要找到符合题意的数组元素的下标,所以先要将原来的数组深拷贝一份,然后排

LeetCode Combination Sum III

原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 与Combination Sum II相似,不同的是中不是所有元素相加,只是k个元素相加. 所以在把item的copy 加到res前需要同时满足item.size() == k 和 target == 0两个条件. AC Java: 1 public class Solution { 2 public List<List<Integer>> combinationS

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution 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

LeetCode: Combination Sum II 解题报告

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 ta