原题链接在这里:https://leetcode.com/problems/reconstruct-itinerary/description/

题目:

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.

题解:

Eulerian path. 把这些ticket当成edge构建directed graph. 保证每条edge 只走一遍.

为了保证字母顺序,用了PriorityQueue.

然后做dfs. dfs 时注意 retrieve nodes backwards.

Time Complexity: O(n+e). Space: O(n+e).

AC Java:

 class Solution {
public List<String> findItinerary(List<List<String>> tickets) {
List<String> res = new ArrayList<>();
if(tickets == null || tickets.size() == 0){
return res;
} HashMap<String, PriorityQueue<String>> graph = new HashMap<>();
for(List<String> e : tickets){
graph.putIfAbsent(e.get(0), new PriorityQueue<String>());
graph.get(e.get(0)).add(e.get(1));
} dfs("JFK", graph, res);
return res;
} private void dfs(String cur, HashMap<String, PriorityQueue<String>> graph, List<String> res){
while(graph.containsKey(cur) && graph.get(cur).size() != 0){
String next = graph.get(cur).poll();
dfs(next, graph, res);
} res.add(0, cur);
}
}

LeetCode 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】332. Reconstruct Itinerary 解题报告(Python)

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

  3. 【LeetCode】Reconstruct Itinerary(332)

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

  4. 【LeetCode】332. Reconstruct Itinerary

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

  5. [leetcode]332. Reconstruct Itinerary

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

  6. 332. Reconstruct Itinerary (leetcode)

    1. build the graph and then dfs -- graph <String, List<String>>,  (the value is sorted a ...

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

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

  8. 332 Reconstruct Itinerary 重建行程单

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

  9. Reconstruct Itinerary

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

随机推荐

  1. storm UI

    Storm UI ——本文主要解释下storm ui上各项属性的含义. 通过http://UI_Server:8080可以打开Storm Web UI看看Storm集群的状态. 1. mainpage ...

  2. samba 挂载 问题

    link: http://www.minunix.com/2013/04/linux-mount-samba/ http://my.oschina.net/laopiao/blog/161648 最近 ...

  3. centos 编译 安装 protobuf

    link:http://dbua.iteye.com/blog/1633079 yum -y install gcc+ gcc-c++ yum -y install make 下载protobuf-2 ...

  4. 编写unit test以及自动化测试WebDriver

    http://msdn.microsoft.com/en-us/library/hh694602.aspx#BKMK_Quick_starts   http://www.seleniumhq.org/ ...

  5. 51Nod 1002 数字三角形 Label:水水水 && 非学习区警告

    一个高度为N的由正整数组成的三角形,从上走到下,求经过的数字和的最大值. 每次只能走到下一层相邻的数上,例如从第3层的6向下走,只能走到第4层的2或9上.      5   8 4  3 6 9 7 ...

  6. 设置 tableview 的背景颜色,总是不生效

    1.只设置了背景图片,却忘记了取消掉 cell 的背景颜色(可以通过层次结构来观察) UIImageView *bgView = [[UIImageView alloc]initWithFrame:s ...

  7. eclipse中中文字体过小

    转自 http://www.cnblogs.com/HD/p/3654139.html

  8. sencha 安装、学习

     sencha touch 是Extjs 的手机版,Extjs是创建富客户端的AJAX应用中的重量级框架,sencha touch当然就是面向触摸设备的重量级js框架,在做基于桌面的网页时经常用的js ...

  9. Windows Phone Data Protection

    To encrypt the PIN // Convert the PIN to a byte[]. byte[] PinByte = Encoding.UTF8.GetBytes(TBPin.Tex ...

  10. Android常用功能代码块

    1.设置activity无标题,全屏 // 设置为无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为全屏模式 getWindow(). ...