순열
def permute(self, nums: List[int]) -> List[List[int]]:
answer = []
prev_elements = []
def dfs(elements):
if len(elements) == 0:
answer.append(prev_elements[:])
return
for e in elements:
next_elements = elements[:]
next_elements.remove(e)
prev_elements.append(e)
dfs(next_elements)
prev_elements.pop()
dfs(nums)
return answer
조합
def combine(self, n: int, k: int) -> List[List[int]]:
answer = []
num_list = [i for i in range(1, n+1)]
def dfs(elements, start, target):
if target == 0 :
answer.append(elements[:])
return
for i in range(start, len(num_list)):
elements.append(num_list[i])
dfs(elements, i+1, target - 1)
elements.pop()
dfs([], 0, k)
return answer
'Programming > Python' 카테고리의 다른 글
[Python] shorten url (base64, url safe base64) (0) | 2022.04.23 |
---|---|
[Python] 피보나치 구현 정리 (0) | 2022.04.10 |
Python이 느린 이유? (0) | 2022.04.02 |
python 다중 할당 동작방식 (0) | 2022.03.23 |
python typing optional (0) | 2022.03.23 |
댓글