国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

332. Reconstruct Itinerary

greatwhole / 1307人閱讀

摘要:題目解答都是用來(lái)解,一個(gè)用一個(gè)用來(lái)實(shí)現(xiàn)深度優(yōu)先搜索,搜索到一個(gè)城市只是的時(shí)候即沒(méi)有出度的時(shí)候,把這個(gè)記入中去,因?yàn)樗隙ㄊ亲詈蟮竭_(dá)的城市,然后依次向前類推的要求在存入的時(shí)候就用先存好先進(jìn)去的說(shuō)明是出發(fā)城市,那么最先出發(fā)的城市最后出來(lái)

題目:
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來(lái)解,一個(gè)用recursion, 一個(gè)用stack來(lái)實(shí)現(xiàn):
Recursion version:

public void dfs(String departure, Map> graph, List result) {
    //深度優(yōu)先搜索,搜索到一個(gè)城市只是arrival city的時(shí)候(即沒(méi)有出度的時(shí)候,把這個(gè)city記入list中去,因?yàn)樗隙ㄊ亲詈蟮竭_(dá)的城市,然后依次向前類推
    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的時(shí)候就用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()) {
        //先進(jìn)去的說(shuō)明是出發(fā)城市,那么最先出發(fā)的城市最后出來(lái)
            stack.push(graph.get(stack.peek()).poll());
        }
        result.add(0, stack.pop());
    }
    return result;
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/64923.html

相關(guān)文章

  • Leetcode[332] Reconstruct Itinerary

    摘要:復(fù)雜度思路重建,應(yīng)為要按進(jìn)行排序,所以用來(lái)決定下一個(gè)要出去的值。 Leetcode[332] Reconstruct Itinerary Given a list of airline tickets represented by pairs of departure andarrival airports [from, to], reconstruct the itinerary ...

    Flands 評(píng)論0 收藏0
  • [LeetCode] Reconstruct Itinerary

    摘要:來(lái)自大神的解答,只能膜拜。題目確定了至少有一條的行程不存在分支情況,一定有相同的最終目的地,而且對(duì)于多條的行程,要選取字母順序較小的一條。 Problem Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the i...

    jubincn 評(píng)論0 收藏0
  • leetcode423. Reconstruct Original Digits from Engl

    摘要:如對(duì)應(yīng)的英文表達(dá)為并繼續(xù)亂序成。要求輸入亂序的英文表達(dá)式,找出其中包含的所有的數(shù)字,并按照從小到大輸出。思路和代碼首先將數(shù)字和英文表示列出來(lái)粗略一看,我們知道有許多字母只在一個(gè)英文數(shù)字中出現(xiàn),比如只出現(xiàn)在中。 題目要求 Given a non-empty string containing an out-of-order English representation of digits...

    kviccn 評(píng)論0 收藏0
  • 基于Keras實(shí)現(xiàn)加密卷積神經(jīng)網(wǎng)絡(luò)

    摘要:奧胡斯大學(xué)密碼學(xué)機(jī)器學(xué)習(xí)工程師介紹了如何實(shí)現(xiàn)基于加密數(shù)據(jù)進(jìn)行訓(xùn)練和預(yù)測(cè)的卷積神經(jīng)網(wǎng)絡(luò)。通過(guò)卷積神經(jīng)網(wǎng)絡(luò)分析圖像在最近幾年極為流行,因?yàn)樵趫D像相關(guān)任務(wù)上的表現(xiàn)超過(guò)了其他許多方法。 奧胡斯大學(xué)密碼學(xué)PhD、Datadog機(jī)器學(xué)習(xí)工程師Morten Dahl介紹了如何實(shí)現(xiàn)基于加密數(shù)據(jù)進(jìn)行訓(xùn)練和預(yù)測(cè)的卷積神經(jīng)網(wǎng)絡(luò)。TL;DR 我們選取了一個(gè)經(jīng)典的CNN深度學(xué)習(xí)模型,經(jīng)過(guò)一系列步驟的改造,使其得以基于...

    fjcgreat 評(píng)論0 收藏0
  • 【問(wèn)題】從一長(zhǎng)串?dāng)?shù)字中找到重復(fù)多次的三個(gè)數(shù)字

    摘要:?jiǎn)栴}描述假設(shè)給定一個(gè)很長(zhǎng)的數(shù)字,比如精確到萬(wàn)位,找到其中重復(fù)出現(xiàn)相鄰三個(gè)數(shù)字。最后我們只要把新序列里統(tǒng)計(jì)值大于的打印出來(lái)即可。我們可以用更加優(yōu)雅的方式來(lái)呈現(xiàn)以上算法,簡(jiǎn)潔但不簡(jiǎn)單。以上便是上原題的最佳答案。 問(wèn)題描述 https://stackoverflow.com/que... 假設(shè)給定一個(gè)很長(zhǎng)的數(shù)字,比如PI精確到100萬(wàn)位,找到其中重復(fù)出現(xiàn)相鄰三個(gè)數(shù)字。比如給定的數(shù)字是1233...

    afishhhhh 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<