leetcode 332 Reconstruct Itinerary

题目连接

https://leetcode.com/problems/reconstruct-itinerary/ 

Reconstruct Itinerary

Description

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.

Note:

  1. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
  2. All airports are represented by three capital letters (IATA code).
  3. You may assume all tickets form at least one valid itinerary.

 

Example 1:
tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Return ["JFK", "MUC", "LHR", "SFO", "SJC"].

Example 2:
tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Return ["JFK","ATL","JFK","SFO","ATL","SFO"].
Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it is larger in lexical order.

题目大意: 从"JFK"出发找出一个序列使得所有的tickets用完,注意所求的结果要求字典序最小.
思路: 深搜,注意要标记已经访问过的节点(回溯时要记得节点还原)。
PS: 用完$n$张票当然需要用$n+1$个点

class Solution {
public:
    vector<string> findItinerary(vector<pair<string, string>> tickets) {
        vector<string> res;
        if(tickets.empty()) return res;
        for(auto &r: tickets) mp[r.first].insert({ r.second, false });
        res.push_back("JFK");
        dfs("JFK", res, tickets.size());
        return res;
    }
    bool dfs(string from, vector<string> &res, int n) {
        if((int)res.size() == n + 1) return true;
        for(auto r = mp[from].begin(); r != mp[from].end(); ++r) {
            string temp = (*r).first;
            auto c = mp[from].find({ temp, false });
            if(c == mp[from].end()) continue;
            res.push_back(temp);
            mp[from].erase(c);
            mp[from].insert({ temp, true });
            if(dfs(temp, res, n)) return true;
            res.pop_back();
            c = mp[from].find({ temp, true });
            if(c == mp[from].end()) continue;
            mp[from].erase(c);
            mp[from].insert({ temp, false });
        }
        return false;
    }
private:
    unordered_map<string, multiset<pair<string, bool>>> mp;
};

转载于:https://www.cnblogs.com/GadyPu/p/5612534.html

7.5华为OD机试真题 新系统 - 省超足球比赛获胜队伍计算 (Java/Py/C/C++/Js/Go) 7.5华为OD机试真题 新系统 - 省超足球比赛获胜队伍计算 (Java/Py/C/C++/Js/Go) 阅读详情

相关推荐

不只是CPU主频:深入解析英飞凌Aurix TC3xx各外设时钟树(CAN、以太网、Flash时序如何配置)

本文深入解析英飞凌Aurix TC3xx系列微控制器的外设时钟树配置,涵盖CAN、以太网和Flash时序的优化方法。通过详细分析时钟系统架构、关键外设配置及多外设协同设计策略,帮助工程师实现高性能汽车电子控制单元(ECU)开发。特别关注系统时钟的灵活配置与EMC优化技巧,提升系统实时性和可靠性。

weixin_29189373的博客 399

Leetcode 332. Reconstruct Itinerary

Leetcode 332. Reconstruct Itinerary dfs:   熟悉深度优先搜索的代码结构,回溯+递归 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.ut

独钓寒江雪 578

Cadence psspxf实战:手把手教你仿真250MHz环形VCO的电源抑制比(附Verilog代码)

本文详细介绍了使用Cadence psspxf仿真器对250MHz环形VCO的电源抑制比(PSRR)进行仿真的全流程。从环形VCO的设计原理、Verilog代码实现,到Cadence环境配置、仿真参数设置,再到结果分析与优化技巧,提供了完整的实战指南。特别适合电路设计工程师学习如何评估和优化VCO的抗电源噪声能力。

weixin_42567752的博客 414

[LeetCode 332] Reconstruct Itinerary

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus

coder 进阶的专栏 2871

5分钟搞定阿里云千问大模型API申请:手把手教你获取API Key

本文提供了一份高效的阿里云通义千问大模型API Key获取实战指南。通过清晰的步骤,帮助开发者在五分钟内完成从账号准备、精准导航控制台到创建并验证API Key的全过程,并强调了密钥安全管理和基础调用测试的重要性,助力快速集成AI能力。

ol789012345的博客 1339

sphincs+ Nist Round3文档中文译版(SPHINCS+ Submission to the NIST post-quantum project, v.3)

1. 引言 哈希基础签名方案最初是由Lamport [14]在1970年代后期作为一次性签名方案开发的,并由Merkle [16]扩展为多次签名方案。这些方案的安全性易于分析,并仅依赖于所使用的哈希函数的属性。然而,Merkle基于树的签名方案需要在密钥生成时就确定要进行签名的数量,且为了保持性能这个数字要小。最重要的是,该系统需要用户记住一个状态:这个状态包含一些信息以记住密钥已经进行了多少次签名。 在Lamport方案诞生后的40年里,产生了许多想法改进了哈希基础签名的性能、实用性和理论基础,最终XMS

dedanddwb的博客 394

Leetcode: 332.Reconstruct Itinerary

Leetcode: 332.Reconstruct Itinerary

qq_16318319的博客 492

clock min pulse width检查

高电平脉宽 = clock fall edge到达FF/CK的时间 - clock rise edge到达FF/CK的时间;低电平脉宽 = 下一T clock rise edge到达FF/CK的时间 - clock fall edge到达FF/CK的时间。而required pulse width是根据FF/CK处的transition,根据查找表计算得到的。为什么clock transition越大,会导致required pulse width越大呢?5、 降低clock上的串扰。

芯冰乐 577

[leetcode] 332. Reconstruct Itinerary

Description Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from J...

Learning from the mistakes 305

ESP32-C3入门教程 WiFi篇①——WiFi Scan 扫描附近AP无线热点 | 堵塞等待扫描结果,回调事件通知扫描结果

文章目录一、快速运行二、运行效果三、程序流畅四、关键函数4.1 扫描所有有效的AP4.2 获取上次扫描中找到的AP列表4.3 获取上次扫描中找到的AP数 一、快速运行 示例项目中,选择WiFi—>scan menuconfig配置ESP32C3-Specific—>Rec 0 芯片选择ESP32-C3(Built-in USB JTAG) 快速运行ESP-IDF Build, Flash and Monitor(左下角) 二、运行效果 三、程序流畅 esp_netif_init,初始

5416

TVS 管击穿电压与钳位电压的工程解析

TVS二极管的核心参数击穿电压(VBR)和钳位电压(VC)常被混淆,但二者具有本质区别。VBR是

硬核科技 476

[LeetCode]332. Reconstruct Itinerary

Reconstruct ltinerary

LadyCaro的博客 439

YOLOv8 智能交通违章检测 - 压线/越线行驶检测详解

本文提出了一种基于YOLOv8的智能交通违章检测方案,重点解决车辆压线/越线行驶的识别问题。系统采用多技术融合方案:YOLOv8检测车辆、ByteTrack跟踪轨迹、传统或深度学习方法识别车道线,并通过几何计算判断车辆是否违规跨越实线。文章详细介绍了技术难点(如车道线分类、透视变形处理)、判定流程、系统架构和核心代码实现,包括车道线检测、违章逻辑判断和可视化模块。同时指出了现有方案的局限性,建议通过引入逆透视变换和专用车道线分割模型来提升准确率。该纯视觉方案具有低成本、易部署的特点,适用于智能交通监控场景。

u010117029的博客 172

[LeetCode] 332. Reconstruct Itinerary

[LeetCode] 332. Reconstruct Itinerary

weixin_39629939的博客 327

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

[leetcode] 332. Reconstruct Itinerary @ python

原题 Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus,...

闲庭信步 564

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

leetcode——332——Reconstruct Itinerary

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus,

happyxuma1991的博客 392

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

[leetcode] 332. Reconstruct Itinerary 解题报告

题目链接:https://leetcode.com/problems/reconstruct-itinerary/ Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order.

小榕流光的专栏 6051

LeetCode 332. Reconstruct Itinerary

I struggled a while for looking the best data structure to do this DFS... the following is the best solution I think.... #include #include #include #include #include #include using namespace st

ActiveCoder的博客 451

【leetcode】332. Reconstruct Itinerary

题目: 思路: 有向图的深度优先搜索。tickets里实际上保存的是图的有向边。我用unordered_map保存每个from节点(出发地)到其所有邻接to节点(目的地,用一个链表将所有邻接to节点按照字符串从小到大的顺序串起来)的映射关系。每次深度优先遍历邻接点时,都临时将节点从对应链表中删除掉,继续进行更深度的递归。如果能成功,即所有地点都串起来了(总数为边的数量加1),则ok;如果不成功,则将删除的节点再添加回去,继续尝试下一个邻接点。如此往复,直到成功。 代码实现: 代码搞了好几个小时,真就一

zxc120389574的博客 290

LeetCode: 332. Reconstruct Itinerary

LeetCode: 332. Reconstruct Itinerary 题目描述 Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belo...

杨领well的专栏 350

LeetCode 332. Reconstruct Itinerary【medium】

原题: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JF

this->ou 460

leetcode [332]Reconstruct Itinerary

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, th...

weixin_30741653的博客 126

LeetCode //C - 332. Reconstruct Itinerary

【代码】LeetCode //C - 332. Reconstruct Itinerary。

Made in Code 837

LeetCode332 Reconstruct Itinerary

find the smallest lexicographic order of a valid plane tickets order starting from “JFK” In graph theory, an Eulerian trail (or Eulerian path) is a trail in a finite graph that visits every edge exact...

Tech in Pieces 348

Leetcode 332. Reconstruct Itinerary[medium]

题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus

Proverbs 433
上一篇: leetcode 226 Invert Binary Tree
下一篇: leetcode 367 Valid Perfect Square
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值