【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, 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.
提示:
这道题看到之后第一反应一般就是使用dfs,不过用法还需要结合题目的要求和图的特性。因为题目要求结果要按照字母大小顺序排序,因此我们在记录每个点能到达的点集时,可以用STL中的unordered_map<string, multiset<string>>, unordered_map效率比map高一些,另外multiset允许同样的值插入多次,且是按序插入的。
然后说说这个图的特性,因为题目说了给定的输入是一定有解的,所以,图中所有的点我们可以按照出度和入度之和的奇偶分成两类:
- 出度和入度之和为奇:这种点最多只有两个,就是起点和终点;
- 出度和入度之和为偶:就是正常的中间过渡点;
- 如果所有点的出度和入度之和都为偶,那么一直dfs到底就是要求的解;
- 在dfs过程中,如果我们stuck了,其实就是因为我们访问到了终点。
上面这几个特性就不一一证明了,可以画个草图简单理解一下。
因此我们在dfs的时候,如果卡住了,那么说明访问到了终点,就把这个点放进vector中。如果没卡住的话,就把点push进stack中(用于回溯),并且一直访问下去,并且经过的点都要记得及时删除,防止走重复的路径。
最后,由于先访问到的“终点”在vector的前端,因此在返回vector前要记得reverse一下。
代码:
class Solution {
public:
vector<string> findItinerary(vector<pair<string, string>> tickets) {
unordered_map<string, multiset<string>> m;
vector<string> res;
if (tickets.size() <= ) {
return res;
}
for (pair<string, string> p: tickets) {
m[p.first].insert(p.second);
}
stack<string> s;
s.push("JFK");
while (s.size()) {
string next = s.top();
if (m[next].empty()) {
res.push_back(next);
s.pop();
} else {
s.push(*m[next].begin());
m[next].erase(m[next].begin());
}
}
reverse(res.begin(), res.end());
return res;
}
};
【LeetCode】332. Reconstruct Itinerary的更多相关文章
- 【LeetCode】332. Reconstruct Itinerary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)
[LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...
- 【leetcode】1253. Reconstruct a 2-Row Binary Matrix
题目如下: Given the following details of a matrix with n columns and 2 rows : The matrix is a binary mat ...
- 【LeetCode】423. Reconstruct Original Digits from English
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)
[LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- Git/Github 教程
转载自 *链接(http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000)*
- 如何使用Handler
什么是Handler? Handler可以发送和处理消息对象或Runnable对象,这些消息对象和Runnable对象与一个线程相关联.每个Handler的实例都关联了一个线程和线程的消息队列.当创建 ...
- 【WPF】三维模型中的“照相机”
WPF 部分支持三维模型,为啥说是部分支持?毕竟 WPF 的侧重点还是在应用开发上,虽然也有些游戏是用 WPF 开发的,不过,老周想啊,如果真要开发游戏,最好用专门的框架,WPF 应当用于开发应用功能 ...
- Hibernate createQuery调用joincolumn
1. Beans a. Version Bean package locationService.beans; import java.sql.Timestamp; import java.util. ...
- Spring MVC 请求处理流程概览
SpringMVC工作流程 图一:请求流程概述 图二:请求在每个组件的处理 解释Spring工作流程 1.用户向服务器发送请求,请求被spring前端控制Servelt DispatcherServe ...
- asp.net已流的方式下载文件
string filePath = context.Server.MapPath("~/" + uploadFolder+"/"+file_name);//路径 ...
- 7.java 加解密技术系列之 AES
java 加解密技术系列之 AES 序 概念 原理 应用 代码实现 结束语 序 这篇文章继续介绍对称加密算法,至于今天的主角,不用说,也是个厉害的角色 — — AES.AES 的出现,就是为了来替代原 ...
- intersect for multiple vectors in R
Say you have a <- c(1,3,5,7,9) b <- c(3,6,8,9,10) c <- c(2,3,4,5,7,9) A straightforward way ...
- C# 类型基础(中)
前一篇文章中我们讲到了值类型和引用类型的一些区别,那这篇我们将深入的分析一下到底有什么不一样 先总结一下两者的差别: 黄金法则: 1.引用类型总是被分配到托管堆上. 2.值类型总是分配到它声明的地方: ...
- windows上使用SecureCRT连接linux
前言: SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件.这样操作的时候不必进入到linux桌面,可以更方便的 ...