Skip to content

Commit ffaa0f7

Browse files
committed
combination-sum solution
1 parent c20a0af commit ffaa0f7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

combination-sum/yyyyyyyyyKim.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
4+
answer = []
5+
6+
# 재귀
7+
def backtrack(start, path, total):
8+
# total이 target과 같아지면 path복사해서 answer에 추가하고 종료
9+
if total == target:
10+
answer.append(path[:])
11+
return
12+
13+
# total이 target값 넘어가면 종료
14+
if total > target:
15+
return
16+
17+
for i in range(start, len(candidates)):
18+
path.append(candidates[i]) # 일단 path에 추가하고
19+
backtrack(i, path, total + candidates[i]) # 검증하기
20+
path.pop() # 마지막 값 꺼내고 다음으로
21+
22+
# backtrack 함수호출
23+
backtrack(0, [], 0)
24+
25+
return answer

0 commit comments

Comments
 (0)