资讯专栏INFORMATION COLUMN

332. Reconstruct Itinerary

greatwhole / 1300人阅读

摘要:题目解答都是用来解,一个用一个用来实现深度优先搜索,搜索到一个城市只是的时候即没有出度的时候,把这个记入中去,因为它肯定是最后到达的城市,然后依次向前类推的要求在存入的时候就用先存好先进去的说明是出发城市,那么最先出发的城市最后出来

题目:
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:
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"].
All airports are represented by three capital letters (IATA code).
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.

解答:
都是用DFS来解,一个用recursion, 一个用stack来实现:
Recursion version:

public void dfs(String departure, Map> graph, List result) {
    //深度优先搜索,搜索到一个城市只是arrival city的时候(即没有出度的时候,把这个city记入list中去,因为它肯定是最后到达的城市,然后依次向前类推
    PriorityQueue arrivals = graph.get(departure);
    while (arrivals != null && !arrivals.isEmpty()) {
        dfs(arrivals.poll(), graph, result);
    }
    result.add(0, departure);
}

public List findItinerary(String[][] tickets) {
    List result = new ArrayList();
    //lexical order的要求在存入graph的时候就用priority queue先存好
    Map> graph = new HashMap<>();
    for (String[] iter : tickets) {
        graph.putIfAbsent(iter[0], new PriorityQueue());
        graph.get(iter[0]).add(iter[1]);
    }
    
    dfs("JFK", graph, result);
    return result;
}

Stack version:

public List findItinerary(String[][] tickets) {
    List result = new ArrayList();
    Map> graph = new HashMap();
    for (String[] iter : tickets) {
        graph.putIfAbsent(iter[0], new PriorityQueue());
        graph.get(iter[0]).add(iter[1]);
    }
    
    Stack stack = new Stack();
    stack.push("JFK");
    while (!stack.isEmpty()) {
        while (graph.containsKey(stack.peek()) && !graph.get(stack.peek()).isEmpty()) {
        //先进去的说明是出发城市,那么最先出发的城市最后出来
            stack.push(graph.get(stack.peek()).poll());
        }
        result.add(0, stack.pop());
    }
    return result;
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64923.html

相关文章

  • Leetcode[332] Reconstruct Itinerary

    摘要:复杂度思路重建,应为要按进行排序,所以用来决定下一个要出去的值。 Leetcode[332] Reconstruct Itinerary Given a list of airline tickets represented by pairs of departure andarrival airports [from, to], reconstruct the itinerary ...

    Flands 评论0 收藏0
  • [LeetCode] Reconstruct Itinerary

    摘要:来自大神的解答,只能膜拜。题目确定了至少有一条的行程不存在分支情况,一定有相同的最终目的地,而且对于多条的行程,要选取字母顺序较小的一条。 Problem Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the i...

    jubincn 评论0 收藏0
  • leetcode423. Reconstruct Original Digits from Engl

    摘要:如对应的英文表达为并继续乱序成。要求输入乱序的英文表达式,找出其中包含的所有的数字,并按照从小到大输出。思路和代码首先将数字和英文表示列出来粗略一看,我们知道有许多字母只在一个英文数字中出现,比如只出现在中。 题目要求 Given a non-empty string containing an out-of-order English representation of digits...

    kviccn 评论0 收藏0
  • 基于Keras实现加密卷积神经网络

    摘要:奥胡斯大学密码学机器学习工程师介绍了如何实现基于加密数据进行训练和预测的卷积神经网络。通过卷积神经网络分析图像在最近几年极为流行,因为在图像相关任务上的表现超过了其他许多方法。 奥胡斯大学密码学PhD、Datadog机器学习工程师Morten Dahl介绍了如何实现基于加密数据进行训练和预测的卷积神经网络。TL;DR 我们选取了一个经典的CNN深度学习模型,经过一系列步骤的改造,使其得以基于...

    fjcgreat 评论0 收藏0
  • 【问题】从一长串数字中找到重复多次的三个数字

    摘要:问题描述假设给定一个很长的数字,比如精确到万位,找到其中重复出现相邻三个数字。最后我们只要把新序列里统计值大于的打印出来即可。我们可以用更加优雅的方式来呈现以上算法,简洁但不简单。以上便是上原题的最佳答案。 问题描述 https://stackoverflow.com/que... 假设给定一个很长的数字,比如PI精确到100万位,找到其中重复出现相邻三个数字。比如给定的数字是1233...

    afishhhhh 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<