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. 单片机之静态局部变量static

    HL-1慧静电子 上程序: main.c #include <reg52.h>#include "Timer.h" /********P1口低有效*********** ...

  2. 容器技术之LXC

    什么是容器?在生活中我们常见的容器有各种瓶瓶罐罐.各种能够容纳其它物料的东西叫容器:容器的特点就是有着很好的隔离作用,使得不同的物料互相隔离:除此之外容器还方便运输.方便储存:这是生活中所说的容器,以 ...

  3. windows假死原因调查

    0. 现象 windows假死了,键盘功能正常,就是画面卡住不动了. 1. 看log linux下面很容易通过命令dmesg和/var/log/message来看日志. 但是windows就懵逼了,不 ...

  4. python re 里面match 和search的区别

    re.match()从开头开始匹配string. re.search()从anywhere 来匹配string. 例子: >>> re.match("c", &q ...

  5. vs2015 cppunit配置及使用

    目录 第一步 第二步 第三步 编译生成lib库 使用 calculator类测试 代码部分 第一步 下载源代码 http://sourceforge.net/projects/cppunit/file ...

  6. DOM面试题

    1.利用冒泡和不利用冒泡的差别 答案: 1.绑定位置不同:不利用冒泡绑定在目标元素上,利用冒泡绑定在父元素上. 2.监听对象的个数不同:不利用冒泡会反复创建多个监听,利用冒泡始终只有 一个监听. 3. ...

  7. js 简单有效判断日期有效性(含闰年)

    原文:https://zhidao.baidu.com/question/1701946584925153620.html 要想精确验证,最容易想到的方法就是通过月份判断日期是否合法(1~28/29/ ...

  8. SQL——CREATE、ALTER、DROP和VIEW

    CREATE DATABASE - 创建新数据库    语法:CREATE DATABASE database_nameALTER DATABASE - 修改数据库    CREATE TABLE - ...

  9. tp5插入百万条数据处理优化

    <?php namespace app\index\controller; use think\Controller; use think\Db; class Charu extends Con ...

  10. 重学 Java 设计模式:实战建造者模式

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 乱码七糟 [luàn qī bā zāo],我时常怀疑这个成语 ...