原题链接在这里: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. 跳转页面&回到上一页

    1.php <?php header('Location:2.php'); //echo不能在其前面 echo '1'; 2.js <?php echo '2'; echo '<me ...

  2. CentOS增加swap分区

    使用dd命令创建一个swap分区 [root@localhost Desktop]#dd if=/dev/zero of=/home/swap bs=1024 count=1048576 count的 ...

  3. Spring In Action ②

    初始化和销毁Bean init-method && destory-method <bean id="auditorium" class="test ...

  4. 让mysql有直接写redis能力

    1.文件包下载 http://pan.baidu.com/s/1qW9DHYc 2.安装 gcc -fPIC -Wall -I/usr/local/mysql/include/mysql -I. -s ...

  5. [小工具]ChemistryHelper

    // 此博文为迁移而来,写于2015年3月25日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vuv7.html 前几天 ...

  6. android实现两个页面跳转

    1.实现两个界面之间转换 在安卓当中,最常见的就是按下按钮之后跳转到第二个界面. 关键代码很简单: 这是以bn2按钮为例,当前Activity为MainActivity,跳转到Registration ...

  7. select..in(参数化) 解决注入式问题

    方案1 为where in的每一个参数生成一个参数,写法上比较麻烦些,传输的参数个数有限制,最多2100个,可以根据需要使用此方案 using (SqlConnection conn = new Sq ...

  8. nodejs怎么同步从一个数据库查询函数中返回一个值

    var sql=require('msnodesql'); var conn_str="Driver={SQL Server Native Client 11.0};Server={127. ...

  9. Android带侧滑菜单和ToolBar的BaseActivity

    写Android的时候,可能有多个界面.在风格统一的软件中,写Activity时会有很多重复.例如我所在软工课程小组的项目:Github链接 ,里面的TaskListActivity和TeacherL ...

  10. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...