332. 重新安排行程

给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 出发。

说明:

如果存在多种有效的行程,你可以按字符自然排序返回最小的行程组合。例如,行程 [“JFK”, “LGA”] 与 [“JFK”, “LGB”] 相比就更小,排序更靠前

所有的机场都用三个大写字母表示(机场代码)。

假定所有机票至少存在一种合理的行程。

示例 1:

输入: [[“MUC”, “LHR”], [“JFK”, “MUC”], [“SFO”, “SJC”], [“LHR”, “SFO”]]

输出: [“JFK”, “MUC”, “LHR”, “SFO”, “SJC”]

示例 2:

输入: [[“JFK”,“SFO”],[“JFK”,“ATL”],[“SFO”,“ATL”],[“ATL”,“JFK”],[“ATL”,“SFO”]]

输出: [“JFK”,“ATL”,“JFK”,“SFO”,“ATL”,“SFO”]

解释: 另一种有效的行程是 [“JFK”,“SFO”,“ATL”,“JFK”,“ATL”,“SFO”]。但是它自然排序更大更靠后。

class Solution {
private Map<String, PriorityQueue<String>> map = new HashMap<>(); private List<String> resList = new LinkedList<>(); public List<String> findItinerary(List<List<String>> tickets) {
for (List<String> ticket : tickets) {
String src = ticket.get(0);
String dst = ticket.get(1);
if (!map.containsKey(src)) {
PriorityQueue<String> pq = new PriorityQueue<>();
map.put(src, pq);
}
map.get(src).add(dst);
}
dfs("JFK");
return resList;
} private void dfs(String src) {
PriorityQueue<String> pq = map.get(src);
while (pq != null && !pq.isEmpty())
dfs(pq.poll());
((LinkedList<String>) resList).addFirst(src);
}
}

Java实现 LeetCode 332 重新安排行程的更多相关文章

  1. Leetcode 332.重新安排行程

    重新安排行程 给定一个机票的字符串二维数组[from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序.所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生 ...

  2. 【LeetCode】重新安排行程

    [问题]给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序.所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生,所 ...

  3. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. STM32 CubeIDE快速创建工程(图文详解)

    使用STM32CubeIDE快速创建STM32的HAL库工程. 文章目录 1 STM32CubeIDE Home 2 生成工程 3 程序下载 1 STM32CubeIDE Home 进入到官网的下载界 ...

  2. STM32 IAP 升级官方资料汇总

    整理了一下SMT32标准外设库进行IAP升级的官方demo: 标准库版本 STM32F10xxx in-application programming using the USART (AN2557) ...

  3. python100例 11-20

    011 兔子问题 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? f1=1 f2=1 for i ...

  4. ScrollView 内嵌百度地图问题解决

    在ScrollView上内嵌百度地图遇到两个问题 事件冲突,移动地图的时候屏幕滚动了 移动ScrollView的时候,百度地图出现黑边 问题1的处理就有各种办法了,核心都是拦截事件,我使用的办法是加一 ...

  5. java -> 异常类与自定义异常

    异常 什么是异常?Java代码在运行时期发生的问题就是异常. 在Java中,把异常信息封装成了一个类.当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如异常出现的位置.原因等). 异常的继承体 ...

  6. sqlservere小计合计总计

    SELECT CASE WHEN GROUPING(F1) = 1 THEN '总计' WHEN GROUPING(F1) = 0 AND GROUPING(F2) = 1 THEN F1+'合计' ...

  7. 枚举:Enum-Int-String之间的转换与扩展

    示例枚举: public enum Color { [Description("红色")] Red, [Description("绿色")] Green = 7 ...

  8. windows下node配置npm全局路径(踩坑)

    事情的起因是:Koa要求v7.6.0以上的nodejs. 但是window环境下升级node不容易,试过npm install -g n 和n stable等命令无效,而网上推荐的nvm并不支持win ...

  9. MyCat基本知识

    一.Mycat基本元素 1.逻辑库,mycat中存在,对应用来说相当于mysql数据库,后端可能对应了多个物理数据库,逻辑库中不保存数据 2.逻辑表,逻辑库中的表,对应用来说相当于mysql的数据表, ...

  10. Redis-主从

    主从复制过程 1.从服务器开始连接主服务器时,会向主服务器发送一个SYNC同步命令 2.主服务器接收到命令后,执行BGSAVE,异步的将写命令保存到一个缓冲区里 3.主服务器执行完BGSAVE之后,就 ...