Coding Practice (03/30)

Date: 2021/3/30

Source: Leetcode No.39 Combination Sum

Language: Python 3

Coding:

 1 class Solution:
 2     def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
 3         ans = []
 4         combination = []
 5         def is_combination(target, candidates, index):
 6             if sum(combination) == target:
 7                 ans.append(combination[:])
 8                 return 
 9             if sum(combination) > target:
10                 return
11             for i in range(index, len(candidates)):
12                 combination.append(candidates[i])
13                 is_combination(target, candidates, i)
14                 combination.pop()
15             
16         is_combination(target, candidates, 0)
17         return ans
原文地址:https://www.cnblogs.com/key1994/p/14596896.html