leetcode Reconsrtuct Itinerary

原创 2016年08月30日 20:26:01

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.

Credits:

Special thanks to @dietpepsi for adding this problem and creating all test cases.

code:

public List<String> findItinerary(String[][] tickets) {
     List<String> list=new ArrayList<>();
    Map<String,Queue<String>> map=new HashMap<>();
    for(String[] str:tickets){
        if(map.get(str[0])==null){
            Queue<String> queue=new PriorityQueue<>(new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return o1.compareTo(o2);
                }
            });
            queue.offer(str[1]);
            map.put(str[0],queue);
        }
        else map.get(str[0]).offer(str[1]);
    }
    Stack<String> stack=new Stack<>();
    stack.push("JFK");
    while(!stack.isEmpty()){
        while(map.containsKey(stack.peek())&&!map.get(stack.peek()).isEmpty()){
            stack.push(map.get(stack.peek()).poll());
        }
        list.add(0,stack.pop());
    }
    return list;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode 332. Reconstruct Itinerary

题目【难度:Medium】:Given a list of airline tickets represented by pairs of departure and arrival airports...

[leetcode] 332. Reconstruct Itinerary 解题报告

题目链接:https://leetcode.com/problems/reconstruct-itinerary/ Given a list of airline tickets represen...

LeetCode 332. Reconstruct Itinerary【medium】

原题: Given a list of airline tickets represented by pairs of departure and arrival airports [fro...

Leetcode 332. Reconstruct Itinerary

332. Reconstruct Itinerary Total Accepted: 19256 Total Submissions: 71899 Difficulty: Medium Giv...

Leetcode——310 Minimum Height Trees && 332 Reconstruct Itinerary

本周记录两道图论的题目,第一道是关于BFS的题目,310, Minimum Height Trees ;第二道是关于DFS的题目,332,Reconstruct Itinerary。 一、Minim...

Leetcode: 332.Reconstruct Itinerary

Leetcode: 332.Reconstruct Itinerary

LeetCode 332 : Reconstruct Itinerary

Reconstruct Itinerary该题是常规的图搜索相关的题目,使用dfs便可以解决,可惜自己还是折腾了好久。 先贴上我原先的代码:#include #include #include ...

LeetCode 332. Reconstruct Itinerary(重构行程)

原题网址:https://leetcode.com/problems/reconstruct-itinerary/ Given a list of airline tickets represen...

Leetcode 332. Reconstruct Itinerary[medium]

题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, t...

leetcode 332. Reconstruct Itinerary

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)