Combination Sum I(可重复取)
Example
[7]
[2, 2, 3]Notice
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.class Solution:
"""
@param candidates: A list of integers
@param target: An integer
@return: A list of lists of integers
"""
def combinationSum(self, candidates, target):
# write your code here
candidates = sorted(list(set(candidates)))#排序和去重复,比如3个1只要一个
ret = []
self.dfs(candidates, target, ret, [], 0)
return ret
def dfs(self,candidates, target, ret, path, start):
if target == 0:
ret.append(list(path))
for i in range(start,len(candidates)):
if candidates[i] > target:#此处可以直接返回因为已排序,当前i不行的话后面更大
return
path.append(candidates[i])
self.dfs(candidates, target-candidates[i], ret, path, i)
path.pop()Last updated