题目:

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;
}
};

最新文章

  1. android 中theme和style的语法相关
  2. win8 系统无法正常安装.net framework 2.0和3.0框架如何解决
  3. html学习记录之表格、表单基础
  4. ural 1431. Diplomas
  5. linux 文件操作命令
  6. 暑假集训单切赛第一场 POJ 2309 BST(找规律的题)
  7. COM 学习小记录
  8. 【JMeter】Jmeter引入第三方jar包
  9. storm学习之入门篇(一)
  10. mysql中数据库database、实例instance、会话session的关系
  11. [HMLY]7.iOS MVVM+RAC 从框架到实战
  12. Codeforces 691A Fashion in Berland
  13. doubango简介
  14. 在JavaScript中使用json.js:使得js数组转为JSON编码
  15. javascript面向对象的写法及jQuery面向对象的写法
  16. HDU 1556 BIT区间修改+单点查询(fread读入优化)
  17. python第一次周末大作业
  18. data.frame类型数据如何将第一列值替换为行号
  19. Windows10 + IntelliJ IDEA 2017.3.2 + wamp2e + xdebug 调试 配置
  20. day2 大纲笔记

热门文章

  1. 通过winform+模拟登录实现快速一键登录到人才招聘网站
  2. 区块链入门(2):搭建以太坊私有链(private network of ethereum),以及挖矿的操作..
  3. 利用gulp搭建简单服务器,gulp标准版
  4. Eclipse中如何关联Javadoc
  5. dockerfile语法
  6. 消息队列RabbitMQ与Spring集成
  7. 022 component(组件)关联映射
  8. kotlin语言使用初体验(一)
  9. Zxing 的集成 ---- Maven 对应 Gradle 的写法
  10. window.opener的用法