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]
相关推荐
言乐635 分钟前
Python概括后端原理
开发语言·python·django·virtualenv·pygame
Y38153266243 分钟前
搜索 API 响应模块:featured_snippet、PAA 与 knowledge_graph 怎么用
python·搜索引擎
10年前端老司机2 小时前
实战分享:基于 PyMuPDF+Qwen-VL 实现图文兼容的 PDF RAG 方案
人工智能·python·agent
2601_962885722 小时前
如何用 Python 回测定投策略(定期定额 DCA)?(和一次性买入对比)
开发语言·人工智能·python
蜗牛互联网2 小时前
Python接入Gemini 3.8 Flash实现票据视觉抽取与规则校验
java·javascript·网络·人工智能·python
小小张说故事2 小时前
Python 虚拟环境到底怎么选?venv/conda/pipenv/poetry 一张对比表 + 9 个踩坑
python
在世修行2 小时前
干货:配置持久化与引擎状态的可视化
python·可视化·持久化
All for pursuit.2 小时前
【回溯-4】46.全排列
数据结构·c++·算法·leetcode
虎大猫猫2 小时前
Python教程——按位取反的原理
开发语言·python
泡茶喝茶写代码2 小时前
A股量化数据工程:从 REST 接口到策略信号(第 4 篇):行业与概念板块树构建
java·python·股票数据api·股票数据·股票数据api接口·股票api数据接口·股票量化数据api