【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 ...
随机推荐
- centos6.5软件安装:RPM,SRPM与yum功能
鸟哥的linxu私房菜读书笔记 前言: Linux上软件的安装可以以原始码的方式来安装软件,也就是利用厂商释出的 Tarball 来进行软件的安装.不过,你应该很容易发现,那就是每次安装软件都需要侦测 ...
- Kafka学习-复制
复制 Kafka可以通过可配置的服务器数量复制每个主题分区的日志(可以为每个主题设置复制因子).这允许在集群中的服务器发生故障时自动故障转移到其他副本,因此在存在故障的情况下,消息仍然可用. 其他消息 ...
- JAVA中的Buffer
一.属性 Buffer有四个基本属性: 1.capacity 容量,buffer能够容纳的最大元素数目,在Buffer创建时设定并不能更改 2.limit buffer中有效位置数目,不能对超过li ...
- Rabin-Karp字符串查找算法
1.简介 暴力字符串匹配(brute force string matching)是子串匹配算法中最基本的一种,它确实有自己的优点,比如它并不需要对文本(text)或模式串(pattern)进行预处理 ...
- elasticsearch系列(三)分表分库
首先ES没有库和表的概念,只有index,type,document(详细术语可以看ES的系列一 http://www.cnblogs.com/ulysses-you/p/6736926.html), ...
- Linux parent process and child process when 'sudo'
如果在一般用户下如user,执行sudo命令,会产生两个进程. ps -ef | grep Container root 4305 643 0 16:37 pts/39 00:00:00 sudo . ...
- nginx配置优化+负载均衡+动静分离详解
nginx配置如下: #指定nginx进程运行用户以及用户组user www www;#nginx要开启的进程数为8worker_processes 8;#全局错误日志文件#debug输出日志最为详细 ...
- SmartCoder每日站立会议02
1.站立会议内容 经过昨天一天的学习,大家了解到了很多新的知识,在各自任务实现方面也遇到了问题,比如首页的css样式实现.API接口的连接和地图接入的样式,经过今日站立会议,大家在一起讨论了各自出现的 ...
- Hibernate与Jpa的关系(2)
[转自:http://blog.163.com/hero_213/blog/static/398912142010312024809/ ] 近年来ORM(Object-Relational Mappi ...
- Creating beautiful charts in chinese with ggplot2
Before we start My chinese skills are poor and biased. I did learn during my internship and I contin ...