dfs(2)
-
Leetcode 46. Permutations
Leetcode 46. Permutations Permutations - LeetCode Permutations - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Logic DFS로 깊이탐색 prev는 추가되고 next는 줄어들면서 dfs를 돈다 Solution from typing import List class Solution: def permute(self, nums: List[int]) -> List[List[int]]: prev = [] answer =..
2021.01.29 -
[알고리즘] DFS, BFS 구현 (Python)
DFS """ 1 / | \ 2 3 4 | | 5 | / \ / 6 7 """ graph = { 1: [2,3,4], 2: [5], 3: [5], 4: [], 5: [6,7], 6: [], 7: [3], } def recursive_dfs(v, discovered = []): discovered.append(v) # 시작 정점 방문 for w in graph[v]: if not w in discovered: # 방문 하지 않았으면 discovered = recursive_dfs(w, discovered) return discovered def iterative_dfs(start_v): discovered = [] stack = [start_v] while stack: v = stack.pop() if v..
2021.01.27