题目:

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.

提示:

这道题看到之后第一反应一般就是使用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的更多相关文章

  1. 【LeetCode】332. Reconstruct Itinerary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 相似题目 参考资料 日期 题目地址:htt ...

  2. 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)

    [LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...

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

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

  5. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. [刷题]算法竞赛入门经典(第2版) 4-8/UVa12108 - Extraordinarily Tired Students

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa12108 - Extraordinarily Tired Stude ...

  2. 蚂蚁金服新一代数据可视化引擎 G2

    新公司已经呆了一个多月,目前着手一个数据可视化的项目,数据可视化肯定要用到图形库如D3.Highcharts.ECharts.Chart等,经决定我的这个项目用阿里旗下蚂蚁金服所开发的G2图表库. 官 ...

  3. Spring BeanFactory源码学习

    一.BeanFactory BeanFactory是Spring IOC容器的基础,是IOC容器的基础接口,所有的容器都是从它这里继承实现而来.BeanFactory提供了最基本的IOC容器的功能,即 ...

  4. kotlin 语言入门指南(三)--编码习惯

    这章主要讲当前 kotlin 的一些编码习惯. 命名 如无疑问,kotlin 的命名风格与以下的java风格一样: --驼峰命名法(不要使用下划线) --类名首字母大写 --方法和属性名首字母小写 - ...

  5. Error:No such property: GROUP for class: org.gradle.api.publication.maven.internal.deployer.DefaultGroovyMavenDeployer

    注:参考:http://stackoverflow.com/questions/28450549/errorno-such-property-group-for-class-org-gradle-ap ...

  6. 用C语言模仿Python函数

    首先得说明一点,C 语言不是函数式编程语言,要想进行完全的函数式编程,还得先写个虚拟机,然后再写个解释器才行(相当于 CPython ). 下面我们提供一个例子,说明 C 语言函数可以"适度 ...

  7. 通过网络得到html,并解析出其中网址(JAVA程序)

    网络版程序: import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; ...

  8. Swift 入门之简单语法(三)

    集合 数组 数组使用 [] 定义,这一点与 OC 相同 //: [Int] let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 遍历 for num in nu ...

  9. springboot 集成elasticsearh的简单配置

    添加依赖 gradle compile("org.springframework.boot:spring-boot-starter-data-elasticsearch:${springBo ...

  10. Redis 小白指南(一)- 简介、安装、GUI 和 C# 驱动介绍

    Redis 小白指南(一)- 简介.安装.GUI 和 C# 驱动介绍 目录 简介 安装 入门指令 GUI 工具 C# 驱动介绍 简介 ANSI C 编写,开源,基于内存,可持久化,一个键值对的数据库, ...