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.
详见:https://leetcode.com/problems/reconstruct-itinerary/description/
C++:
class Solution {
public:
vector<string> findItinerary(vector<pair<string, string> > tickets) {
vector<string> res;
unordered_map<string, multiset<string> > m;
for (auto a : tickets)
{
m[a.first].insert(a.second);
}
dfs(m, "JFK", res);
return vector<string> (res.rbegin(), res.rend());
}
void dfs(unordered_map<string, multiset<string> > &m, string s, vector<string> &res) {
while (m[s].size())
{
string t = *m[s].begin();
m[s].erase(m[s].begin());
dfs(m, t, res);
}
res.push_back(s);
}
};
参考:https://www.cnblogs.com/grandyang/p/5183210.html
332 Reconstruct Itinerary 重建行程单的更多相关文章
- [LeetCode] Reconstruct Itinerary 重建行程单
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 【LeetCode】332. Reconstruct Itinerary
题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to ...
- [leetcode]332. Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 【LeetCode】332. Reconstruct Itinerary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 相似题目 参考资料 日期 题目地址:htt ...
- 332. Reconstruct Itinerary
class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...
- 332. Reconstruct Itinerary (leetcode)
1. build the graph and then dfs -- graph <String, List<String>>, (the value is sorted a ...
- Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...
- LeetCode Reconstruct Itinerary
原题链接在这里:https://leetcode.com/problems/reconstruct-itinerary/ 题目: Given a list of airline tickets rep ...
随机推荐
- 多校 1010 Taotao Picks Apples(补题)
>>点击进入原题<< 思路:题解很有意思,适合线段树进阶 考虑每次修改不叠加,因此我们可以从如何对原序列进行预处理着手.通过观察可以发现,将原序列从任意位置断开,我们可以通过分 ...
- 表格头部header固定
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Django Rest FrameWork再练习
可能有重构目前应用的需求,rest framework是值得有必要深入去了解的. 所以,这应该是第三次看官方文档来练习, 希望能获取更深入的记忆. __author__ = 'CHENGANG882' ...
- [JavaEE] Bootstrapping a JavaEE Application
To bootsrap the application use the following Maven archetype: mvn -DarchetypeGroupId=org.codehaus.m ...
- Java Map 怎样实现Key 的唯一性?
大家都知道.在Map和Set不可存在反复元素? 可是对于内部的细节我们并不了解.今天我们就一块来 探讨一下! 1 对于 HashMap HashSet 他们的底层数据结构的实现是:维护了一张 Ha ...
- Python3基础(十一) 类的拓展
在类的初印象中,我们已经简单的介绍了类,包括类的定义.类对象和实例对象.本文将进一步学习类的继承.迭代器.发生器等等. 一.类的继承 单继承 派生类的定义如下: class DerivedClassN ...
- C++学习之函数模板与类模板
泛型编程(Generic Programming)是一种编程范式,通过将类型参数化来实现在同一份代码上操作多种数据类型,泛型是一般化并可重复使用的意思.泛型编程最初诞生于C++中,目的是为了实现C++ ...
- J2EE基础总结(5)——EJB
什么是EJB JB事实上就是企业Java Beans. EJB是J2EE平台的重要组成部分. J2EE平台基于组件的企业级应用架构,提供多 层次.分布式和高事务的功能特点. EJB提供 ...
- 【转】MySQL随机字符串生成
DROP FUNCTION IF EXISTS rand_string; DELIMITER $$ CREATE FUNCTION rand_string(str_length TINYINT UNS ...
- Android学习之仿QQ側滑功能的实现
如今项目越来越多的应用了滑动删除的功能,Android本来遵循的是长按删除,IOS定制的是滑动删除,不可否认滑动删除确实在客户体验上要好一点,所以看了非常多关于仿QQ滑动删除的样例,还是感觉代码家的A ...