LeetCode 332. Reconstruct Itinerary重新安排行程 (C++/Java)
题目:
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:
Input:[["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Output:["JFK", "MUC", "LHR", "SFO", "SJC"]
Example 2:
Input:[["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Output:["JFK","ATL","JFK","SFO","ATL","SFO"]
Explanation: Another possible reconstruction is["JFK","SFO","ATL","JFK","ATL","SFO"].
But it is larger in lexical order.
分析:
给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 出发。
可以将题目理解为一个有向图,飞机票当成图的边,最后求的是结点的顺序,实际上是求一个欧拉回路。

这里我们使用Hierholzer算法求解此问题。
正常来说我们应该先求出度为奇数的点,不过由于这道题告知要从“JFK”开始,所以我们可以直接从JFK开始搜索。
DFS(u):
while(u存在未被访问过的边e(u, v))
标记边e(u, v)已被访问
DFS(v)
END
将点u添加到路径集中
还是以上面的为例,从JFK开始,存在未被访问的边(1,3),我们在这个选择3,也就是通往SFO的边(注意此题要求应该是选择字符排序小的点,这里只是模拟一下求解欧拉回路的过程),然后我们将3这条边标记以访问。
然后从SFO开始,存在为被访问的边(4),我们选择4这条边,到达了ATL这个点,同样的4也被标记访问过了。
ATL存在未被访问的边(5,2),我们选择5这条边,到达了SFO这个点,5也被标记访问过。
SFO已经不存在未被访问的边了(4已经被标记访问过了),所以我们将SFO加入到路径集中[SFO],并返回上次访问的点。
此时ATL中还存在2这条边未被访问,我们选择2这条边,到达了JFK这个点,2也标记访问过。
JFK中1还未访问,我们选择1这条边,到达了ATL这个点,注意此时所有的边都已经访问过了,ATL没有边可以继续访问了,我们将ATL加入路径集[SFO,ATL],返回上次访问的点。
此时JFK也没有边访问了,我们将JFK加入[SFO,ATL,JFK]
同理ATL也没有可访问的边了,将ATL加入[SFO,ATL,JFK,ATL]
返回到SFO,也没有边可以访问了,将SFO加入[SFO,ATL,JFK,ATL,SFO]
最后我们回到了出发点JFK,1,3都已被标记访问过,将JFK加入到路径集中得[SFO,ATL,JFK,ATL,SFO,JFK],最后将结果集中数据反转一下即可得到所求得欧拉路径。也就是JFK->SFO->ATL->JFK->ATL->SFO
不过注意由于题中要求字符自然排序最小,所以我们在选择边时,要按照顺序选在下一个访问的结点。例如从JFK开始有通向SFO和ATL两个边,我们选择通往ATL的边,依照这样的规则我们可以得到结果
["JFK","ATL","JFK","SFO","ATL","SFO"]

小技巧:在保存机票起点和终点时,我们可以使用有限队列存储边,优先访问字符小的边。
程序:
C++
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
for(int i = 0; i < tickets.size(); ++i){
if(map.find(tickets[i][0]) == map.end()){
priority_queue <string, vector<string>, cmp> q;
q.push(tickets[i][1]);
map[tickets[i][0]] = q;
}
else{
map[tickets[i][0]].push(tickets[i][1]);
}
}
findPath("JFK");
reverse(res.begin(), res.end());
return res;
}
void findPath(string begin){
while(map.find(begin) != map.end() && map[begin].size() != 0){
string next = map[begin].top();
map[begin].pop();
findPath(next);
}
res.push_back(begin);
}
private:
struct cmp
{
bool operator() (string a, string b)
{
return a > b;
}
};
vector<string> res;
unordered_map<string, priority_queue <string, vector<string>, cmp>> map;
};
Java
class Solution {
public List<String> findItinerary(List<List<String>> tickets) {
for(List<String> pair:tickets){
String key = pair.get(0);
String value = pair.get(1);
if(!map.containsKey(key)){
PriorityQueue<String> p = new PriorityQueue<>();
p.add(value);
map.put(key, p);
}
else{
map.get(key).add(value);
}
}
getPath("JFK");
return res;
}
private void getPath(String begin){
while(map.containsKey(begin) && map.get(begin).size() != 0){
getPath(map.get(begin).poll());
}
res.add(0, begin);
}
private List<String> res = new ArrayList<>();
private Map<String, PriorityQueue<String>> map = new HashMap<>();
}
LeetCode 332. Reconstruct Itinerary重新安排行程 (C++/Java)的更多相关文章
- [leetcode]332. Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 【LeetCode】332. Reconstruct Itinerary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...
- 【LeetCode】332. Reconstruct Itinerary
题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to ...
- 332. Reconstruct Itinerary (leetcode)
1. build the graph and then dfs -- graph <String, List<String>>, (the value is sorted a ...
- 332 Reconstruct Itinerary 重建行程单
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 332. Reconstruct Itinerary
class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...
- Java实现 LeetCode 332 重新安排行程
332. 重新安排行程 给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序.所有这些机票都属于一个从JFK(肯尼迪国际机场 ...
- Leetcode 332.重新安排行程
重新安排行程 给定一个机票的字符串二维数组[from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序.所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生 ...
- [Swift]LeetCode332. 重新安排行程 | Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
随机推荐
- 第四課-Channel Study File Reader & File Writer
示例描述:从数据库中读取数据并过滤转换为HL7并存放到指定目录;然后读取目录中的HL7文件转换为txt文本并存放到指定目录. 首先在F:\MirthConnect\Test目录下创建Out目录存放输出 ...
- 力扣283(java)-移动零(简单)
题目: 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 请注意 ,必须在不复制数组的情况下原地对数组进行操作. 示例 1: 输入: nums = [0, ...
- Git 工具下载慢问题 & 图像化界面工具
Git 命令行淘宝镜像:git-for-windows Mirror (taobao.org) Git 图形客户端:Download – TortoiseGit – Windows Shell Int ...
- 如何在 Linux 上部署 RabbitMQ
如何在 Linux 上部署 RabbitMQ 目录 如何在 Linux 上部署 RabbitMQ 安装 Erlang 从预构建的二进制包安装 从源代码编译 Erlang RabbitMQ 的安装 使用 ...
- aspnetcore项目中kafka组件封装
前段时间在项目中把用到kafka组件完全剥离开出来,项目需要可以直接集成进去.源代码如下: liuzhixin405/My.Project (github.com) 组件结构如下,代码太多不一一列举, ...
- 使用 @NoRepositoryBean 简化数据库访问
在 Spring Data JPA 应用程序中管理跨多个存储库接口的数据库访问逻辑可能会变得乏味且容易出错.开发人员经常发现自己为常见查询和方法重复代码,从而导致维护挑战和代码冗余.幸运的是,Spri ...
- Java ”框架 = 注解 + 反射 + 设计模式“ 之 反射详解
Java "框架 = 注解 + 反射 + 设计模式" 之 反射详解 每博一文案 无论幸福还是苦难,无论光荣还是屈辱,你都要自己遭遇与承受. ------ <平凡的世界> ...
- 一、Doris演进史
Apache Doris -- 为分析而生 Doris发展历程: Doris发展比较重要的关键节点与事件 #2008 - Doris1 :「筑巢引凤」的重要基石 早年,百度最主要的收入来源是广告.广告 ...
- Linux(三):Linux的目录及相关作用
使用 Linux,不仅限于学习各种命令,了解整个 Linux 文件系统的目录结构以及各个目录的功能同样至关重要.使用 Linux 时,通过命令行输入 ls -l / 可以看到,在 Linux 根目录( ...
- IPv6 — 地址格式与寻址模式
目录 文章目录 目录 前文列表 IPv6 的地址格式 站点前缀 地址生成方式 IPv6 地址的分类以及寻址模式 单播(Unicast)地址 Interface ID 全球唯一地址(Global Uni ...