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. 太极旋转-JS实现

    刚学了js的一些函数,所以做了一个太极的旋转.做完之后是上面这个样子的,是可以旋转的. 思路: 1.先做一个基准转盘,之后将元素都放在转盘上,跟随转盘动. 2.画两个半圆,主要属性是border-to ...

  2. 【转】Web测试方法

    看到好文章,拿过来给大家分享分享! 一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&am ...

  3. IOS网络第七天WebView-02WebView和网页的交互2,删除大众点评多余文字,加上蒙版进度

    ************ #import "HMViewController.h" @interface HMViewController () <UIWebViewDele ...

  4. Visual Studio “14” CTP 4

    微软发布于10月6日发布了Visual Studio "14"CTP 4,本次发布的更新主要包括:ASP.NET vNext runtime和一些工具的优化(ASP.NET vNe ...

  5. DevOps是云计算时代的开发与运营

    DevOps(英文Development和Operations的组合)是一组过程.方法与系统的统称,用于促进开发(应用程序/软件工程).技术运营和质量保障(QA)部门之间的沟通.协作与整合.[1] 它 ...

  6. iconfont的蜕化操作

    很多国外的网站,访问的时候可以看到,页面先是大面积白一下,然后恢复正常.原因是网页上用到了 webfont,这些页面很多情况都是直接引用 google 的 webfont 地址,中华大局域网下,由于网 ...

  7. [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦

    [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦 本节导读:上篇文章简单介绍了.NET面向对象中一个重要的技术反射的基本应用,它可以让我们动态的调 ...

  8. ajax的post方式和get方式比较,以及需要注意的地方

    说明:测试所用的js框架为kissy,后端语言为php 写在前面 目前我们可以将ajax请求的情形按照不同的类型进行分类,比如页面编码:utf-8 or gbk; ajax 传参方式 post or ...

  9. defered,promise回顾

    defered,promise回顾 http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_o ...

  10. Zabbix实现微信报警

    一.  申请企业微信账号,申请地址 https://qy.weixin.qq.com/ 二. 登陆企业微信账 图一 图二 2.添加微信账号 图一 图二 完成以上步骤后 就完成了微信账号的添加 三.新建 ...