Python | Leetcode Python题解之第332题重新安排行程

题目:

题解:

python 复制代码
class Solution:
    def findItinerary(self, tickets: List[List[str]]) -> List[str]:
        def dfs(curr: str):
            while vec[curr]:
                tmp = heapq.heappop(vec[curr])
                dfs(tmp)
            stack.append(curr)

        vec = collections.defaultdict(list)
        for depart, arrive in tickets:
            vec[depart].append(arrive)
        for key in vec:
            heapq.heapify(vec[key])
        
        stack = list()
        dfs("JFK")
        return stack[::-1]
相关推荐
xoliu1几秒前
Pytorch核心基础入门
人工智能·pytorch·python
一瞬祈望4 分钟前
ResNet50 图像分类完整实战(Notebook Demo + 训练代码)
人工智能·python·神经网络·数据挖掘
其美杰布-富贵-李6 分钟前
PyTorch Lightning Callback 指南
人工智能·pytorch·python·回调函数·callback
_codemonster17 分钟前
python易混淆知识点(十六)lambda表达式
开发语言·python
superman超哥1 小时前
仓颉Option类型的空安全处理深度解析
c语言·开发语言·c++·python·仓颉
2401_841495641 小时前
【LeetCode刷题】跳跃游戏Ⅱ
数据结构·python·算法·leetcode·数组·贪心策略·跳跃游戏
Data_agent1 小时前
OOPBUY模式淘宝1688代购系统搭建指南
开发语言·爬虫·python
张哈大1 小时前
AI Ping 上新限免:GLM-4.7 与 MiniMax-M2.1 实测对比
人工智能·python
乘凉~1 小时前
【Linux作业】Limux下的python多线程爬虫程序设计
linux·爬虫·python
xwill*1 小时前
pytorch中项目配置文件的管理与导入方式
人工智能·python