题目:

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. SpringData系列四 @Query注解及@Modifying注解

    @Query注解查询适用于所查询的数据无法通过关键字查询得到结果的查询.这种查询可以摆脱像关键字查询那样的约束,将查询直接在相应的接口方法中声明,结构更为清晰,这是Spring Data的特有实现. ...

  2. PHP 底层的运行机制与原理 --转

    发现一片总结的还不错的文章,记录一下 PHP说简单,但是要精通也不是一件简单的事.我们除了会使用之外,还得知道它底层的工作原理. PHP是一种适用于web开发的动态语言.具体点说,就是一个用C语言实现 ...

  3. layui框架部分功能介绍

    注意:代码的所有功能都没有导入layui的css样式 一,分页功能 layui框架分页使用,其实layui分页非常简单只需要传入一个总页数就可以很好运用这个功能 下面就看一下我对layui框架分页的介 ...

  4. LINQ之LINQ to Objects(上)

    LINQ概述 LINQ,语言集成查询(Language Integrated Query),它允许使用C#或VB代码以查询数据库相同的方式来操作不同的数据源. 1.LINQ体系结构 从上图可以看出,L ...

  5. 使用公共 Registry - 每天5分钟玩转 Docker 容器技术(19)

    保存和分发镜像的最直接方法就是使用 Docker Hub. Docker Hub 是 Docker 公司维护的公共 Registry.用户可以将自己的镜像保存到 Docker Hub 免费的 repo ...

  6. Akka(2):Actor生命周期管理 - 监控和监视

    在开始讨论Akka中对Actor的生命周期管理前,我们先探讨一下所谓的Actor编程模式.对比起我们习惯的行令式(imperative)编程模式,Actor编程模式更接近现实中的应用场景和功能测试模式 ...

  7. 使用gzip优化web应用(filter实现)

    相关知识: gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,返回到客户端,客户端加载后渲染呈现,这种情况文件一般都比较大,如果 ...

  8. Java微服务框架

    Java的微服务框架dobbo.spring boot.redkale.spring cloud 消息中间件RabbitMQ.Kafka.RocketMQ

  9. Interactive pivot tables with R(转)

    I love interactive pivot tables. That is the number one reason why I keep using spreadsheet software ...

  10. ideal导入非maven工程-zdy

    最近在老总给了一个ant部署的项目,发现ideal并没有支持ant直接一键自动配置的功能,like maven一样的,你们懂得.所以在这里记录一下我的心酸. 项目总体结构,webapp准备在ideal ...