【LeetCode】Reconstruct Itinerary(332)
1. 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:
- 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"]. - All airports are represented by three capital letters (IATA code).
- 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.
2. Answer
import java.util.*;
public class Solution {
public List<String> findItinerary(String[][] tickets) {
List<String> result = new ArrayList<String>();
if(tickets == null || tickets.length == 0){
return result;
}
Map<String, ArrayList<String>> graph = new HashMap<String, ArrayList<String>>();
for(int i=0; i<tickets.length; i++){
if(!graph.containsKey(tickets[i][0])){
ArrayList<String> adj = new ArrayList<String>();
adj.add(tickets[i][1]);
graph.put(tickets[i][0], adj);
}else{
ArrayList<String> newadj = graph.get(tickets[i][0]);
newadj.add(tickets[i][1]);
graph.put(tickets[i][0], newadj);
}
}
for(ArrayList<String> a : graph.values()){
Collections.sort(a);
}
Stack<String> stack = new Stack<String>();
stack.push("JFK");
while(!stack.isEmpty()){
while(graph.containsKey(stack.peek()) && !graph.get(stack.peek()).isEmpty()){
stack.push(graph.get(stack.peek()).remove(0));
}
result.add(0,stack.pop());
}
return result;
}
}
【LeetCode】Reconstruct Itinerary(332)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Happy Number(easy)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- 【LeetCode】Counting Bits(338)
1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- 【LeetCode】Self Crossing(335)
1. Description You are given an array x of n positive numbers. You start at point (0,0) and moves x[ ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
随机推荐
- js中的时间与毫秒数互相转换
1.js毫秒时间转换成日期时间 var oldTime = (new Date("2012/12/25 20:11:11")).getTime(); //得到毫秒数 //不 ...
- C#:获取设备电量相关信息
更多资源:http://denghejun.github.io [DllImport("kernel32.dll",EntryPoint="GetSystemPowerS ...
- CSS篇之DIV+CSS布局
<div></div> div与其他标签一样,也是一个XHTML所支持的标签. div是XHTML中指定的,远门用于布局设计的容器标记. 简单的CSS布局 头部 内容 页脚 & ...
- ENode框架Conference案例分析系列之 - Quick Start
前言 前一篇文章介绍了Conference案例的架构设计,本篇文章开始介绍Conference案例的代码实现.由于代码比较多,一开始就全部介绍所有细节,估计很多人接受不了,也理解不了.所以,我先进行一 ...
- Backbone源码分析(三)
Backbone源码分析(一) Backbone源码分析(二) Backbone中主要的业务逻辑位于Model和Collection,上一篇介绍了Backbone中的Model,这篇文章中将主要探讨C ...
- MySQL 远程连接(federated存储引擎)
标签:federated存储引擎 概述 本文主要介绍通过federated存储引擎建立远程连接表 测试环境:mysql 5.6.21 步骤 开启federated存储引擎 先查看federated存储 ...
- 将word文档A表格中的内容拷贝到word文档B表格中
Function IsFileExists(ByVal strFileName As String) As Boolean ) <> Empty Then IsFileExists = T ...
- Go项目结构和模块导入
Go项目结构和模块导入 golang项目结构与其他语言类似,但是仍然有一些需要注意的地方. 项目结构 环境配置 go 命令依赖一个重要的环境变量:$GOPATH,它表示GO项目的路径,如下设置 exp ...
- JavaScript实现TwoQueues缓存模型
本文所指TwoQueues缓存模型,是说数据在内存中的缓存模型. 无论何种语言,都可能需要把一部分数据放在内存中,避免重复运算.读取.最常见的场景就是JQuery选择器,有些Dom元素的选取是非常耗时 ...
- Mint Linux 安装 DotnetCore 遭遇无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系
evlon@evlon-ThinkPad-T530 ~ $ apt install dotnet-dev-1.0.0-preview2-003121 正在读取软件包列表... 完成 正在分析软件包的依 ...