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. jquery 回到顶部,简洁大方

    效果

  2. xamarin 学习

    http://www.cnblogs.com/lonelyxmas/p/5174934.html http://www.cnblogs.com/phytan/p/xamarincrack2.html ...

  3. easyUI datagrid 根据查询条件 选中对应数据的行

    开始 输入了 土豆,南瓜,再次是小青菜,每次输入点击搜索的时候(模糊查询),选中的当前数据对应的行 在做之前,在网上查询了许多资料,也在技术群里问过许多次,弄了好久终于好了. 第一次写博客真不知道写啥 ...

  4. Nova PhoneGap框架 第八章 滚动条

    你可能会疑惑为什么滚动条这么常见的功能会在这里单独列出,但如果你有过PhoneGap开发经验的话,你就会发现要在Android 2.3 里面实现滚动条那真不是一件容易的事. 8.1 概述 目前主流的P ...

  5. Verlet-js JavaScript 物理引擎

    subprotocol最近在Github上开源了verlet-js.地址为https://github.com/subprotocol/verlet-js.verlet-js是一个集成Verlet的物 ...

  6. (源码下载)高灵活度,高适用性,高性能,轻量级的 ORM 实现

    我在上一篇博客中简单说明了一个面向内存数据集的“ORM”的实现方法,也提到我的设计实现或许不能称之为“ORM”,姑且称之为 S-ORM吧. 可能有些小伙伴没有理解我的思路和目的,与传统ORM框架做了简 ...

  7. awk 留底

      序   因为经常做awk编码,而且跨过一段时间就容易忘记,故在此做个留底.便于翻阅.——后期会在这个页面不断补充!   常用常量   属性 描述 NR  已读入的总记录数  ARGIND  当前被 ...

  8. web项目ajax技术一些总结

    WEB项目中,最主要的就是前后端间的联络.有时需要不进行页面跳转的前提下请求后端方法(action),就需要用到ajax. 在这个博客中,我用到的都是原生的js的ajax,不是很喜欢用jquery的a ...

  9. 2013 duilib入门简明教程 -- 简单控件介绍 (12)

        前面的教程应该让大家对duilib的整体有所映像了,下面就来介绍下duilib具体控件的使用.     由于官方没有提供默认的控件样式,所以我就尽量使用win7或者XP自带的按钮样式了,虽然界 ...

  10. 原生JS下拉加载插件分享。

    无聊写了一个JS下拉加载插件,有需要的可以下载. // 使用 // new ManDownLoad("#ul","json/load.json",functio ...