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]
相关推荐
CodexDave24 分钟前
Python 自动化接单实战(一):把 CSV 需求做成配置驱动解析器
java·python·自动化·json·数据清洗·python自动化·csv处理
m沐沐39 分钟前
【深度学习】循环神经网络RNN——结构、原理与长期依赖问题解析
人工智能·pytorch·python·rnn·深度学习·算法·机器学习
风吹心凉1 小时前
python3基础2026.7.28
开发语言·python
Rabitebla1 小时前
C++ 内存管理全面复习:从内存分布到 operator new/delete
java·c语言·开发语言·c++·算法·leetcode
玖玥拾1 小时前
LeetCode 58 最后一个单词的长度
算法·leetcode
曲无忆1 小时前
LLMs赋能依赖类型证明自动化
python
一次旅行1 小时前
act本地预跑GitHub Actions:Python项目完整CI/CD流水线实战
python·ci/cd·github
hanlin031 小时前
刷题笔记:力扣第19题-删除链表的倒数第N个结点
笔记·leetcode·链表
玉鸯1 小时前
RAG 全链路调优指南:从 Chunking 到 Reranker
python·llm·agent
圣光SG2 小时前
Java操作题练习(二)
java·开发语言·python