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:

  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.

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)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  5. 【LeetCode】Counting Bits(338)

    1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...

  6. 【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[ ...

  7. 【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 ...

  8. 【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 ...

  9. 【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 ...

随机推荐

  1. 【转】Polya定理

    转自:http://endlesscount.blog.163.com/blog/static/82119787201221324524202/ Polya定理 首先记Sn为有前n个正整数组成的集合, ...

  2. 2016/11/17 周四 <javascript的封装简单示例>

    这是一个简单的javascript代码封装的示例以及封装后的调用方法: var ticker={ n:0, add:function() { this.n++; }, show:function() ...

  3. VB6.0 为批量字体改名

    从网上下载了一个字符包,解压以后查看,发现文件名是这种形式:0120_XXXXXX_GBK.ttf,看上去很不雅观.我想改成 XXXXXX简体.ttf 这种形式,但字体有300多个,手动修改太浪费时间 ...

  4. 实践.Net Core在Linux环境下的第一个Hello World

    基础环境和相关软件准备 1.CentOS7.1 64位系统(或者其他CentOS版本的64位系统) 2.WinSCP软件(主要是方便管理和编辑Linux系统的文件) 3.XShell软件(Window ...

  5. RemoteIE 开发者可跨平台使用IE测试网页

    RemoteIE,这是一个基于微软Azure的服务,它允许开发者在最新版本的IE(Windows 10技术预览版)中测试他们的网页,而不需要安装或在虚拟机中设置对应的系统.要想使用这项服务,开发者需要 ...

  6. 初试Office 365企业版E3

    Microsoft Office 365 团队给了所有现任的MVP 12个月的微软 Office 365 企业 E3 订阅,今天激活账号并试用了一下,发现非常强大,本文简要介绍下Office 365 ...

  7. 给你的应用“一只”智慧的眼睛 —— Barcode常识普及以及识别信息处理

    在“如何用MediaCapture解决二维码扫描问题”这篇文章中,我们通过“成像”.“截图”与“识别”三个步骤介绍了使用MediaCapture扫码的主要过程及注意事项.本文主要针对“识别”的过程,对 ...

  8. 你必须知道的指针基础-1.预备篇:搭建GCC开发环境

    一.关于GCC编译器 GCC(GNU Compiler Collection)是一套功能强大.性能优越的编程语言编译器,它是GNU计划的代表作品之一.GCC是Linux平台下最常用的编译器,GCC原名 ...

  9. 轻量级ORM框架初探-Dapper与PetaPoco的基本使用

    一.EntityFramework EF是传统的ORM框架,也是一个比较重量级的ORM框架.这里仍然使用EF的原因在于为了突出轻量级ORM框架的性能,所谓有对比才有更优的选择. 1.1 准备一张数据库 ...

  10. Chrome 控制台新玩法-console显示图片以及为文字加样式

    有兴趣的同学可以文章最后的代码复制贴到控制台玩玩. Go for Code 在正常模式下,一般只能向console 控制台输出简单的文字信息.但为了把信息输出得更优雅更便于阅读,除了cosole.lo ...