[leetcode] 1436. Destination City
Description
You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city.
It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
Example 1:
Input: paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
Output: "Sao Paulo"
Explanation: Starting at "London" city you will reach "Sao Paulo" city which is the destination city. Your trip consist of: "London" -> "New York" -> "Lima" -> "Sao Paulo".
Example 2:
Input: paths = [["B","C"],["D","B"],["C","A"]]
Output: "A"
Explanation: All possible trips are:
"D" -> "B" -> "C" -> "A".
"B" -> "C" -> "A".
"C" -> "A".
"A".
Clearly the destination city is "A".
Example 3:
Input: paths = [["A","Z"]]
Output: "Z"
Constraints:
- 1 <= paths.length <= 100
- paths[i].length == 2
- 1 <= cityAi.length, cityBi.length <= 10
- cityAi != cityBi
- All strings consist of lowercase and uppercase English letters and the space character.
分析
題目的意思是:給了一個出發地到目的地的陣列,然後找出最後的目的地字串。我的思路也是很直接,用字典構建一個有向圖,然後從任意幾點出發,深度遍歷有向圖,直到找到最終的城市為止。
看了一下別人實現的,思路基本差不多。
程式碼
class Solution:
def destCity(self, paths: List[List[str]]) -> str:
d={}
for path in paths:
d[path[0]]=path[1]
des=paths[0][0]
while(des in d):
des=d[des]
return des
參考文獻
相關文章
- 【Leetcode】1029. Two City SchedulingLeetCode
- LeetCode 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance??LeetCode
- I'm shocked that they chose for the City to be the central hub
- ABAP和Java的destination和JNDIJava
- SpringBoot @JmsListener(destination = ) 執行時動態修改Spring Boot
- 如何在SAP Server Side JavaScript裡消費destinationServerIDEJavaScript
- SAP Business Application Studio和SAP雲平臺DestinationAPP
- codeforces Round #681 (Div. 2) 1443B Saving the City
- 尋找是否存在從source到destination的路徑
- 問題解決:TNS-12543: TNS:destination host unreachable
- 路由表構成簡介(Destination/Gateway/Genmask/Iface)路由Gateway
- Unity匯出到Xcode沒有Run Destination的問題UnityXCode
- [題解]AT_abc281_g [ABC281G] Farthest City
- 【iOS採坑記錄】Xcode run destination 中沒有 iOS SimulatorsiOSXCode
- 讓SAP雲平臺上的Web應用使用destination服務Web
- 使用SAP雲平臺的destination消費Internet上的OData service
- SAP UI5應用訪問OData metadata的url和DestinationUI
- 邏輯STANDBY建立中碰到ORA-16146: standby destination control file enqueue unavailableENQAI
- SAP Cloud Platform上Destination屬性為odata_gen的具體用途CloudPlatform
- 上線運營一年後,《Knockout City》經歷了哪些變化?
- 能ping通虛擬機器,但snmp報文 Destination unreachable(Host administratively prohibited虛擬機
- springboot專案一直在列印Redis連結資訊Reconnecting, last destination wasSpring BootRedisAST
- 《Knockout City》開發者:遊戲即將停運,我們做錯了什麼?遊戲
- 【LeetCode】如何學習LeetCode?LeetCode
- City Index:2023年全球市值企業排行榜 蘋果以3.03萬億美元領先Index蘋果
- 截至2023年7月遊戲《Frozen City》全球下載量分額(附原資料表) 遊戲
- 截至2023年7月遊戲《Frozen City》全球內購收入分額(附原資料表) 遊戲
- 遊戲城市設計:《賽博朋克2077》夜之城(Night City)的心理感受和分析優化遊戲優化
- leetcodeLeetCode
- 使用 ABAP 事物碼 SM59 建立 Destination 來讀取外網的資料試讀版
- 韓國將建設K-City測試場地專供無人駕駛汽車使用
- LeetCode in actionLeetCode
- leetcode 238LeetCode
- LeetCode 164 最大間距 HERODING的LeetCode之路LeetCode
- LeetCode 143 重排連結串列 HERODING的LeetCode之路LeetCode
- LeetCode問題LeetCode
- 【LeetCode】Jewels and StonesLeetCode
- Leetcode 513 javascriptLeetCodeJavaScript