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]
相关推荐
HHHHH1010HHHHH4 分钟前
JavaScript中利用IIFE创建私有命名空间的经典方案
jvm·数据库·python
木子墨5169 分钟前
LeetCode 热题 100 精讲 | 矩阵与图论进阶篇:矩阵置零 · 螺旋矩阵 · 旋转图像 · 搜索二维矩阵 II · 岛屿数量 · 腐烂的橘子
c++·算法·leetcode·矩阵·力扣·图论
干洋芋果果9 分钟前
前端学python
开发语言·前端·python
YJlio10 分钟前
1 1.2 Windows 账户的分类:管理员 / 标准 / 来宾 + 微软账户 vs 本地账户
人工智能·python·microsoft·ai·chatgpt·openai·agent
篮子里的玫瑰18 分钟前
Python与网络爬虫——列表与元组
开发语言·爬虫·python
knight_9___22 分钟前
RAG面试篇8
人工智能·python·面试·agent·rag
2301_7965885023 分钟前
Go语言如何压缩文件_Go语言gzip压缩教程【基础】
jvm·数据库·python
m0_6178814225 分钟前
c++如何通过重定向rdbuf来捕获第三方库的日志输出到文件【详解】
jvm·数据库·python
Greyson134 分钟前
mysql查询执行过程中如何追踪耗时_使用PROFILE分析指令周期
jvm·数据库·python
Anesthesia丶37 分钟前
Qwen2.5-1.5b 模型部署与LORA训练笔记
pytorch·python·lora·llm·qwen·vllm