class Solution:
def findItinerary(self, tickets):
targets = defaultdict(list) # 创建默认字典,用于存储机场映射关系
for ticket in tickets:
targets[ticket[0]].append(ticket[1]) # 将机票输入到字典中
for key in targets:
targets[key].sort(reverse=True) # 对到达机场列表进行字母逆序排序
result = []
self.backtracking("JFK", targets, result) # 调用回溯函数开始搜索路径
return result[::-1] # 返回逆序的行程路径
def backtracking(self, airport, targets, result):
while targets[airport]: # 当机场还有可到达的机场时
next_airport = targets[airport].pop() # 弹出下一个机场
self.backtracking(next_airport, targets, result) # 递归调用回溯函数进行深度优先搜索
result.append(airport) # 将当前机场添加到行程路径中
class Solution:
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
self.adj = {}
# sort by the destination alphabetically
# 根据航班每一站的重点字母顺序排序
tickets.sort(key=lambda x:x[1])
# get all possible connection for each destination
# 罗列每一站的下一个可选项
for u,v in tickets:
if u in self.adj: self.adj[u].append(v)
else: self.adj[u] = [v]
# 从JFK出发
self.result = []
self.dfs("JFK") # start with JFK
return self.result[::-1] # reverse to get the result
def dfs(self, s):
# if depart city has flight and the flight can go to another city
while s in self.adj and len(self.adj[s]) > 0:
# 找到s能到哪里,选能到的第一个机场
v = self.adj[s][0] # we go to the 1 choice of the city
# 在之后的可选项机场中去掉这个机场
self.adj[s].pop(0) # get rid of this choice since we used it
# 从当前的新出发点开始
self.dfs(v) # we start from the new airport
self.result.append(s) # after append, it will back track to last node, thus the result list is in reversed order