摘要:題目解答都是用來(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 ListfindItinerary(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
摘要:復(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 ...
摘要:來(lái)自大神的解答,只能膜拜。題目確定了至少有一條的行程不存在分支情況,一定有相同的最終目的地,而且對(duì)于多條的行程,要選取字母順序較小的一條。 Problem Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the i...
摘要:如對(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...
摘要:奧胡斯大學(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ò)一系列步驟的改造,使其得以基于...
摘要:?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...
閱讀 728·2023-04-25 20:32
閱讀 2287·2021-11-24 10:27
閱讀 4532·2021-09-29 09:47
閱讀 2251·2021-09-28 09:36
閱讀 3648·2021-09-22 15:27
閱讀 2768·2019-08-30 15:54
閱讀 380·2019-08-30 11:06
閱讀 1278·2019-08-30 10:58