[LeetCode] 39. Combination Sum (Python)
- Reference
νμ΄μ¬ μκ³ λ¦¬μ¦ μΈν°λ·°μ± μμλ_sumμ λΉΌμ£Όλ©΄μ 0μ κΈ°μ€μΌλ‘ λ°±νΈλνΉ ν΄μ€
Solution
from typing import List
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
answer = []
def dfs(index: int, _sum: int, elements: List[int]) -> None:
if _sum > target:
return
# λ°±νΈλνΉ
if _sum == target:
answer.append(elements[:])
return
for i in range(index, len(candidates)):
dfs(i, _sum + candidates[i], elements + [candidates[i]])
dfs(0, 0, [])
return answer
Leave a comment