"Leetcode" reconstruct itinerary (332)

Source: Internet
Author: User

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 departs from JFK . Thus, the itinerary must begin with JFK .

Note:

    1. If There is multiple valid itineraries, you should return the itinerary that have the smallest lexical order when read as A single string. For example, the itinerary have ["JFK", "LGA"] a smaller lexical order than ["JFK", "LGB"] .
    2. All airports is represented by three capital letters (IATA code).
    3. 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's larger in lexical order.

2. Answer

ImportJava.util.*; Public classSolution { PublicList<string>finditinerary (string[][] tickets) {List<String> result =NewArraylist<string>(); if(Tickets = =NULL|| Tickets.length = = 0){            returnresult; } Map<string, arraylist<string>> graph =NewHashmap<string, arraylist<string>>();  for(inti=0; i<tickets.length; i++){            if(!graph.containskey (tickets[i][0]) {ArrayList<String> adj =NewArraylist<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 =NewStack<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 ()); }        returnresult; }}

"Leetcode" reconstruct itinerary (332)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.