Leetcode 46. Permutations
2021. 1. 29. 11:57ㆍProblem Solving/LeetCode
Leetcode 46. Permutations
Logic
- DFS로 깊이탐색
- prev는 추가되고 next는 줄어들면서 dfs를 돈다
Solution
from typing import List
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
prev = []
answer = []
def dfs(elements: List[int]):
if len(elements) == 0:
answer.append(prev[:])
for e in elements:
next = elements[:]
prev.append(e)
next.remove(e)
dfs(next)
prev.pop()
dfs(nums)
return answer
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Leetcode 104. Maximum Depth of Binary Tree (0) | 2021.02.08 |
---|---|
[Leetcode] 347. Top K Frequent Elements (0) | 2021.01.26 |
[Leetcode] 771. Jewels and Stones (0) | 2021.01.19 |
[Leetcode] 706. Design HashMap (0) | 2021.01.19 |
[Leetcode] 641. Design Circular Deque (0) | 2021.01.19 |