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.
重建行程单,在图中找一条路径,能经过所有的边。
参考:http://www.cnblogs.com/grandyang/p/5183210.html
class Solution{
public:
vector<string> findItinerary(vector<pair<string,string>> tickets){\
unordered_map<string,multiset<string>> m;
for(auto t : tickets){
m[t.first].insert(t.second);
}
vector<string> res;
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);
}
};
Reconstruct Itinerary的更多相关文章
- [LeetCode] 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 ...
- 【LeetCode】332. Reconstruct Itinerary
题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to ...
- [Swift]LeetCode332. 重新安排行程 | 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], r ...
- 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>& ...
随机推荐
- 为TIF、JPG图片添加地理坐标/平面直角坐标
图片分辨率.(X方向像素数numX,Y方向像素数numY) 步骤: (1)在放图片的目录下新建TXT文本文档,将文件名改为与图片相同,扩展名改为jgw(JPG图片),(TIF要改为tfw). (2)用 ...
- Spring Framework的核心:IOC容器的实现
2.1 Spring IoC容器概述 2.1.1 IoC容器和依赖反转模式 依赖反转的要义,如果合作对象的引用或依赖关系的管理由具体对象来完成,会导致代码的高度耦合和可测性的降低.依赖控制反转的实 ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
- Android网络连接判断与处理
博客分类: Android 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission android:name="android ...
- AWS-CDH5.5安装 CM配置节点
在CM节点上进行安装时出现错误: ================================================================================ Pa ...
- linux内核学习之六 进程创建过程学习
一 关于linux进程概念的补充 关于进程的基本概念这里不多说,把自己的学习所得作一些补充: 1. 在linux内核中,系统最多可以有64个进程同时存在. 2.linux进程包含的关键要素:一段可执行 ...
- IOS 使用wxsqlite3为sqlite3数据库加密
1,下载wxsqlite3 地址http://jaist.dl.sourceforge.net/project/wxcode/Components/wxSQLite3/wxsqlite3-3.1.1. ...
- mysql 操作杂记
SHOW VARIABLES LIKE 'character%'; SET character_set_client = utf8; SET character_set_connection = ut ...
- strace命令使用
在调试的时候,strace能帮助你追踪到一个程序所执行的系统调用.当你想知道程序和操作系统如何交互的时候,这是极其方便的,比如你想知道执行了哪些系统调用,并且以何种顺序执行. 这个简单而又强大的工具几 ...
- boost和C++11中的sleep
boost boost线程中表示睡眠的函数有sleep和sleep_for sleep 例如: boost::this_thread::sleep(boost::posix_time::seconds ...