原题链接在这里: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. ccc 模拟重力

    x=x+v v=v+gr cc.Class({ extends: cc.Component, properties: { velocity:{ default:null }, grivatity:{ ...

  2. 使用React重构百度新闻webapp前端

    http://wangfupeng.coding.me/share/2016/08/06/restruct-bdnews-webapp-by-react.html

  3. BZOJ3555 [Ctsc2014]企鹅QQ 题解

    题目大意: 有一些字符串,求其中两个等长且恰好只有一位不同的字符串的对数. 思路: Hash大法好!正着倒着各来一遍(底数不同),之后枚举不同的那一位,前后两段拼起来之后为了有区分前面一部分再乘一个数 ...

  4. BZOJ 1798 题解

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 5531  Solved: 1946[Submit ...

  5. 关于transition回调函数的几种写法

    平时工作中经常遇到需要transition动画结束后触发某个功能的问题,但是在映像中好像只见过animate的回调函数, 而transition的很多属性无法在animate中使用,经过一些总结归纳, ...

  6. SQLServer 客户端远程访问配置

    SQL2008报错“请验证实例名称是否正确并且SQL Server已配置为允许远程连接” 第一步: 连接远程服务器时SQL2008报错“请验证实例名称是否正确并且SQL Server已配置为允许远程连 ...

  7. DropDownList 控件不能触发SelectedIndexChanged 事件

    相信DropDownList 控件不能触发SelectedIndexChanged 事件已经不是什么新鲜事情了,原因也无外乎以下几种: 1.DropDownList 控件的属性 AutoPostBac ...

  8. spring mvc 拦截器 拦截子目录

    项目中碰到这一个问题: 对于/user/loginpage,/user/login这一类的url,放行: 对于/user/{userId}/xxx(xxx不为空)的操作,需要拦截,url-patter ...

  9. [LintCode] Submatrix Sum 子矩阵之和

    Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...

  10. Java_Eclipse安装Git插件

    一.从官网选择系统版本下载Git并安装 地址:https://git-scm.com/downloads/ 二.打开Eclipse 1. 第一种安装方法: help-->Install New ...