LeetCode332. Reconstruct Itinerary

idea 编译打包nacos2.0.3源码,生成可执行jar 包常见问题 阅读详情

题目:

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. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.

Note:
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”].
All airports are represented by three capital letters (IATA code).
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.

思路:

题意:给一些机票,出发机场和到达机场表示方式[from,to],用完这些机票,并且行程必须从JFK开始。
注意:如果存在多条有效路径,选择字典序最小的那个。

感谢:http://blog.csdn.net/murmured/article/details/50641289

诶,STL还是不咋会用啊,题还是简单。
建图的时候,顺便按字典序排好。
然后开始不断的dfs选地点加入到栈里面,进入到下一层里面,根据之前选的地点,找与它相连的点,判断是否能加入到结果栈中,如果能又继续递归往下找,如果最后不符合,回溯还原处理。

代码:

bool dfs(vector<string>& res,string curPos,int n,unordered_map<string,map<string,int>>& m){
    if (res.size() == n){
        return true;
    }
    for (auto ticket = m[curPos].begin(); ticket != m[curPos].end();ticket++){
        if (ticket->second != 0){
            res.push_back(ticket->first);
            ticket->second--;
            if (dfs(res, ticket->first, n, m)){
                return true;
            }
            res.pop_back();
            ticket->second++;
        }
    }
    return false;
}
vector<string> findItinerary(vector<pair<string, string>> tickets) {
    unordered_map<string, map<string, int>> m;
    vector<string> res;
    int len = tickets.size();
    if (len == 0){
        return res;
    }
    for (int i = 0; i <len; i++){
        m[tickets[i].first][tickets[i].second]++;
    }
    string start = "JFK";
    res.push_back(start);
    dfs(res,start,len+1,m);
    return res;
}
树莓派3-改装wifi外置天线 声明:本文只是技术性探讨,改装引起的产品损坏等后果,由用户自负! 改装主要有两种方法 方法一:直接替换原来的天线 1、移除原有的小陶瓷天线,就在Made in PRC(或UK)那边的那个 清理一下阻焊剂方便天线接地(图中PRC左侧) 2、焊接天线 天线的屏蔽层焊接到清理完阻焊剂的地方,中间的信号线焊接到PRC右侧焊盘。 3、最终的样子 阅读详情

相关推荐

Gartner DSG数据安全治理 架构及解读

目录背景:架构图: 解释:英文解释:(自上而下)平衡业务与风险、威胁、合规之间的关系(5个维度之间的平衡)优先处理数据集制定安全策略实施安全工具策略配置同步从上到下,从需求调研开始实施数据安全治理。千万不要跨过数据摸底、治理优先级分析、制定治理整体策略,而直接从技术工具开始对数据安全进行治理。 先明确选择需要治理的对象,优先选择重要数据进行数据安全治理工作。可以通过数据梳理、分类分级等手段,完成数据资产盘点和筛选,在纷杂的数据中理清头绪。

锦慈的技术博客 7229

332. Reconstruct Itinerary(重建行程)

332. Reconstruct Itinerary(重建行程)Reconstruct Itinerary重建行程 题目链接 题目描述 题目分析 方法一深搜回溯 算法描述 方法二深搜 算法描述 参考代码 方法一深搜回溯 方法二分治算法题目链接https://leetcode.com/problems/reconstruct-itinerary/description/题目描述 Given a l

qq_32142535的博客 789

深入解析STM32驱动AT25DF321A FLASH芯片的SPI通信与数据完整性验证

本文深入解析STM32通过SPI通信驱动AT25DF321A FLASH芯片的全过程,包括硬件连接、驱动函数实现与数据完整性验证。重点介绍SPI配置、读写操作优化及CRC校验方法,确保数据存储的可靠性,为嵌入式存储系统开发提供实用指南。

agile9scrum的博客 1071

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

zshouyi的博客 307

深入理解 HTTP 协议中的 Content-Type

是 HTTP 协议中的一个头部字段(Header),用于指示请求或响应中实体主体(body)的媒体类型(Media Type),也称为MIME 类型(Multipurpose Internet Mail Extensions)。

h a 6 6 6 c k 为 你解决复杂难题! 1013

NR PUCCH(一) PUCCH format 0/1

NR中PUCCH物理信道用来发送上行控制信息Uplink Control Information(UCI),当然UCI也可以在PUSCH上发送。UCI 内容包括:CSI,HARQ ACK/NACK ,SR 及上述三者的组合信息。 那先看下PUCCH format ,序列的生成及资源映射的相关内容。 NR中支持5种格式PUCCH,根据PUCCH format占用时域符号长度分为短PUCCH(1-2个符号,PUCCH 0/2)和长PUCCH(4-14个符号,PUCCH1/3/4)。使用短PUCCH 能够支

asd199086的博客 6064

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 ...

fighting! 763

31、贝叶斯认知建模与隐马尔可夫模型:理论与应用

本文探讨了贝叶斯认知建模与隐马尔可夫模型(HMM)的理论基础及其在认知科学和神经科学中的应用。重点介绍了贝叶斯定理和HMM的基本原理,并结合实际案例,如情绪识别系统和脑机接口(BCI),展示了它们在处理复杂认知任务中的强大能力。文章还讨论了贝叶斯-HMM模型的技术细节、优势与局限性,以及未来的发展前景。

meat5的博客 152

LeetCode 332. Reconstruct Itinerary

LeetCode 332 因为给的这个题目是确保有解的,这样的话,从起点开始肯定是可以到达每个点,并且肯定只有一个终点。通过DFS的方式,我们可以找到这个终点,一旦不能再去其他的地方,我们可以把终点加入到我们的path里面进去,同时从图上去掉终点。回退到进入终点的点,看是否还能去其他地方,如果不能,也可以加入到这个path。如果中间点还可以去其他的地方,那么在加入path的时候,需要插入到去终点之前的位置。 def findItinerary(self, tickets: List[List[str

frankguodongchen的博客 129

PCIe协议之 Equalization篇 之 谈谈几种模式

对于两端有能力最高支持 32 GT/s 及以上速率的设备,如果把目标速度设置为 8 GT/s 或 16 GT/s 速率,是不建议采用 Bypass-Equalization-to-Highest-Rate 模式的,即便采用了,也不会针对 8 GT/s 或 16 GT/s 的目标速率进行 EQ。对于支持 32 GT/s 及以上速率的设备,其可能根本无需工作在 8 GT/s 或 16 GT/s 速率,这时再针对 8 GT/s 或 16 GT/s 速率进行 EQ,多少有点浪费。

那么菜的博客 581

14、5G物理层的灵活解决方案:广义频分复用(GFDM)

广义频分复用(GFDM)作为一种高度灵活的5G物理层多载波调制方案,能够满足多样化应用场景的需求,包括高吞吐量的比特管道通信、低功耗高连接数的物联网、超低延迟的触觉互联网以及广覆盖的无线区域网。通过参数化配置,GFDM可适配LTE资源网格,实现与现有系统的兼容与共存。其支持多种解调方式、干扰消除技术、OQAM映射及预编码机制,兼具低带外发射、高频谱效率和强鲁棒性。GFDM还能统一描述OFDM、FBMC、FTN等多种经典与先进波形,成为5G及未来通信系统中极具潜力的通用波形框架。

gold8的专栏 116

leetcode 332 Reconstruct Itinerary

Reconstruct Itinerary

GadyPu的专栏 641

【RKNN】RKNN(Rockchip Neural Network)​​ 的详解

RKNN 为 Rockchip 硬件提供了高效的 AI 推理解决方案,适合需要低功耗、实时响应的边缘场景

浩瀚之水的专栏 1704

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

独钓寒江雪 580

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

[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 进阶的专栏 2873

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Leetcode: 332.Reconstruct Itinerary

Leetcode: 332.Reconstruct Itinerary

qq_16318319的博客 493

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

[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 307

[LeetCode]332. Reconstruct Itinerary

Reconstruct ltinerary

LadyCaro的博客 439

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

[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

[LeetCode] 332. Reconstruct Itinerary

[LeetCode] 332. Reconstruct Itinerary

weixin_39629939的博客 327

[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

题目: 思路: 有向图的深度优先搜索。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
上一篇: Cocos2d-x中的缓存
下一篇: 图论面试题
_IanXiao
博客等级 码龄12年 84粉丝 241原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址