视频讲解
代码
class Solution:
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
g = collections.defaultdict(dict)
for u,v in tickets:
if v not in g[u]:
g[u][v] = 0
g[u][v] += 1
print(g)
res = None
def dfs(g, curr, path):
nonlocal res
if res :
return
if len(path) == len(tickets) + 1:
res = path[:]
for neighbor in sorted(g[curr].keys()):
if g[curr][neighbor] == 0:
continue
g[curr][neighbor]-=1
path.append(neighbor)
dfs(g, curr = neighbor, path = path)
g[curr][neighbor] += 1
path.pop()
dfs(g, "JFK", ['JFK'])
return res
PPT讲解
本题涉及的知识点:
- [()][]
我的 Leetcode 讲解频道
代码链接
面试高频考点Youtube链接
- 数组(Array)
- 哈希表(Hash Table)
- 链表(Linked List)
- 数学(Math)
- 链表(Linked List)
- 双指针(Two Pointers)
- 字符串(String)
- 二分查找(Binary Search)
- 分治(Divide and Conquer)
- 动态规划(Dynamic Programming)
- 回溯(Backtracking)
- 栈(Stack)
- 堆(Heap)
- 贪心算法(Greedy)
- 排序(Sort)
- 树(Tree)
- 深度优先搜索(Depth-First Search)
- 广度优先搜索(Breadth-First Search)
- 二叉查找树(Binary Search Tree)
- 递归(Recursion)
- 队列(Queue)
- 移动窗口(Sliding Window)