题目
思路
DFS
代码
class Solution:
"""
@param: candidates: A list of integers
@param: target: An integer
@return: A list of lists of integers
"""
def __init__(self):
self.res_list = []
def dfs(self, candidates, target, startIndex, num_list):
if target < 0: return False
if target == 0:
self.res_list.append(num_list[:])
return True
for i in range(startIndex, len(candidates)):
num_list.append(candidates[i])
self.dfs(candidates, target - candidates[i], i, num_list)
num_list.pop(-1)
def combinationSum(self, candidates, target):
# write your code here
candidates.sort()
self.dfs(candidates, target, 0, [])
return self.res_list