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.

重建行程单,在图中找一条路径,能经过所有的边。

参考: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的更多相关文章

  1. [LeetCode] Reconstruct Itinerary 重建行程单

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  2. 【LeetCode】Reconstruct Itinerary(332)

    1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...

  3. LeetCode Reconstruct Itinerary

    原题链接在这里:https://leetcode.com/problems/reconstruct-itinerary/ 题目: Given a list of airline tickets rep ...

  4. 【LeetCode】332. Reconstruct Itinerary

    题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to ...

  5. [Swift]LeetCode332. 重新安排行程 | Reconstruct Itinerary

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  6. [leetcode]332. Reconstruct Itinerary

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  7. 332 Reconstruct Itinerary 重建行程单

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

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

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

  9. 332. Reconstruct Itinerary

    class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...

随机推荐

  1. [CentOS] 指定命令别名:Alias & 软链接生成命令 ln -s

    参考:CentOS里alias命令详解 每天一个linux命令(35):ln 命令 1. Alias命令 功能描述:我们在进行系统的管理工作一定会有一些我们经常固定使用,但又很长的命令.那我们可以给这 ...

  2. css外边距margin

  3. 新手要想学好Linux系统就必须做好这四件事情

    一般情况下,大部分人接触Linux的机会并不多,对Linux平台下的开发更是一无所知.而现在的发展趋势却越来越表明:无论是作为一个优秀的软件开发人员,或是互联网.IT行业的从业人员,掌握Linux是一 ...

  4. asp.net MVC之 自定义过滤器(Filter) - shuaixf

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...

  5. 模仿JavaAppArguments.java示例,编写一个程序,此程序从命令行接收多个数 字,求和之后输出结果,写出其的设计思想、程序流程图、源程序代码。

    一 设计思想 首先现在file中建立一个类,并把任务名和类名写上(注意类名的大写):第二步则是参数的输入,并且定义求和变量:第三步则是对参数数据类型的要求,要把字符类型转化为整数类型并输出(这也是本道 ...

  6. Java Mysql分页显示

    public class View { private int currentPage; private int pageSize; private int recordCount; public V ...

  7. Bootstrap<基础十三> 按钮组

    按钮组允许多个按钮被堆叠在同一行上.当你想要把按钮对齐在一起时,这就显得非常有用.你可以通过Bootstrap 按钮(Button) 插件 添加可选的 JavaScript 单选框和复选框样式行为. ...

  8. java中equals和“==”补充

    在JDK1.5之后,Integer添加了自动装箱,其形式为Integer i = 5; 装箱过程在内存中是  i = new Integer(4);大家都很熟悉这个吧. 当使用这中形式的时候,equa ...

  9. Fragment的onResume

    需求:Fragment每次由不可见到可见时的回调. 可能最先想到的是onResume方法,实际使用中Fragment的onResume调用时机与其Activity一致,因此类似与viewPager搭配 ...

  10. C# unity3d 贪吃蛇 游戏 源码 及其感想

    这个游戏的设计过程是这样的: 1,创建