언어: python3
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
end = len(candidates)
result = []
candidates = sorted(candidates)
def dfs(start:int,cur:List[int],remain:int):
if remain ==0:
result.append(cur[:])
return
if remain < candidates[start]:
return
for i in range(start,end):
if target >= candidates[i]:
cur.append(candidates[i])
dfs(i,cur,remain-candidates[i])
cur.pop()
dfs(0,[],target)
return result