Posted on 2023-12-15 16:28
Uriel 閱讀(39)
評論(0) 編輯 收藏 引用 所屬分類:
閑來無事重切Leet Code 、
Hash
給出每一條路的起始和終止城市(string),輸出唯一的終點城市(沒有從該城市出發的任何路),數據保證結果唯一,兩個hash表,一個存所有出現過的城市,一個存所有作為過起點的城市
1 #1436
2 #Runtime: 34 ms (Beats 59.12%)
3 #Memory: 13.6 MB (Beats 19.34%)
4
5 class Solution(object):
6 def destCity(self, paths):
7 """
8 :type paths: List[List[str]]
9 :rtype: str
10 """
11 st = set()
12 cities = set()
13 for x, y in paths:
14 st.add(x)
15 cities.add(x)
16 cities.add(y)
17 for x in cities:
18 if x not in st:
19 return x