Scribbling

LeetCode: 40. Combination Sum II 본문

Computer Science/Coding Test

LeetCode: 40. Combination Sum II

focalpoint 2021. 9. 7. 21:19
class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        candidates.sort()
        self.ret = []
        self.dfs(candidates, target, [])
        return self.ret
        
    def dfs(self, candidates, target, path):
        if target < 0:
            return
        if target == 0:
            self.ret.append(path)
            return
        prev = None
        for i, candidate in enumerate(candidates):
            if candidate != prev:
                prev = candidate
                self.dfs(candidates[i+1:], target-candidate, path+[candidate])

'Computer Science > Coding Test' 카테고리의 다른 글

LeetCode: 47. Permutations II  (0) 2021.09.07
LeetCode: 46. Permutations  (0) 2021.09.07
37. Sudoku Solver  (0) 2021.09.06
36. Valid Sudoku  (0) 2021.09.06
44. Wildcard Matching  (0) 2021.09.06