332. Reconstruct Itinerary (leetcode)

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.

开始以为是寻找最短路径,发现有很大区别,比如有重复节点等等。不过写到最后自己在idea上运行通过,还是花了一些时间:

其中用了异常来跳出递归,这也是在网上看到的一种跳出递归的方法,此前没有用过,学习了。

(我自己写遍历经常直接写拼音。。。这个习惯不太好,以后也会注意。。)

public class Solution {
    static List<String> xulie(String a,String[][] ti,int n){
        ArrayList L= new ArrayList();
        for(int i = 0;i<n;i++){
            if(ti[i][0].equals(a)){
                L.add(ti[i][1]);
            }
        }
        if(L.size()>1){
            Collections.sort(L);
        }

        return L;
    }
    static void bianli(String a,String[][] tickets,List<String> h,Map<String,Integer> map,int n,int dp,Map<String,String> mape){
        List<String> L= new ArrayList();
        int p =0;

            L = xulie(a,tickets,n);
            int k =L.size();
            for(int t=0;t<k;t++){
                String b = L.get(t);
                if(map.get(b)==null){
                    if(dp == n-1){
                         h.add(b);
                     throw new StopMsgException();
                    }else{
                        continue;
                    }
                }else{
                    p = (int)map.get(b);
                if(p==0 || mape.get(a).toString().equals(b)==false){
                    a = b;
                    h.add(a);
                     map.put(a,1);
                      mape.put(a,b);
                    dp++;
                    bianli(a,tickets,h,map,n,dp,mape);
                }

                }

            }

    }
    static void start(Map<String,Integer> map,String[][] tickets,int n){
        for(int i = 0;i<n;i++){
            map.put(tickets[i][0],0);
        }
    }
    public List<String> findItinerary(String[][] tickets) {
        int n = tickets.length;
        String a,b;
        int dp = 0;
        a="JFK";
        List<String> H= new ArrayList();
        H.add(a);
        Map map =new HashMap();
        start(map,tickets,n);
       Map mape =new HashMap();
        try {
             bianli(a,tickets,H,map,n,dp,mape);
        } catch (StopMsgException e) {
            return H;
        }
        return H;
    }

    static class StopMsgException extends RuntimeException {
    }
}
时间: 2024-10-11 17:13:13

332. Reconstruct Itinerary (leetcode)的相关文章

119. Pascal&#39;s Triangle II(LeetCode)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? class Solution { public: vector<int> getRow(int rowIndex) { vector<int&

(LeetCode)杨辉三角形Pascal&#39;s Triangle

题目如下: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 实现代码如下: public class Solution { public List<List<Integer>> generate(int numRows) { Lis

第15个算法-实现 Trie (前缀树)(LeetCode)

解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert("apple"

不邻接植花(leetcode)

有 N 个花园,按从 1 到 N 标记.在每个花园中,你打算种下四种花之一. paths[i] = [x, y] 描述了花园 x 到花园 y 的双向路径. 另外,没有花园有 3 条以上的路径可以进入或者离开. 你需要为每个花园选择一种花,使得通过路径相连的任何两个花园中的花的种类互不相同. 以数组形式返回选择的方案作为答案 answer,其中 answer[i] 为在第 (i+1) 个花园中种植的花的种类.花的种类用  1, 2, 3, 4 表示.保证存在答案. 示例 1: 输入:N = 3,

[LeetCode] 332. Reconstruct Itinerary Java

题目: 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 ther

leetcode 332 Reconstruct Itinerary

题目连接 https://leetcode.com/problems/reconstruct-itinerary/ Reconstruct Itinerary 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 belo

332. Reconstruct Itinerary

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 ar

领扣(LeetCode)二维区域和检索 个人题解

给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8. 示例: 给定 matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5] ] sumRegi

120.三角形最短路径(leetcode)

给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 法1): 本题典型的回溯算法,但是没有剪枝,在42/43个case的时候超时了,以下是代码. PS:尝试过如果当前和大于全局最小,则停止,但是由于有负数的存在,现在大的值也可以通过-9999成为最小值,剪枝失败. class Solution: def minimumTotal(self, triangle: List[List[int]]) -> int: if not triangle or not trian