1436. 旅行终点站
为保证权益,题目请参考 1436. 旅行终点站(From LeetCode).
解决方案1
Python
python
# 1436. 旅行终点站
# https://leetcode-cn.com/problems/destination-city/
################################################################################
from typing import List
class Solution:
def destCity(self, paths: List[List[str]]) -> str:
d = dict(paths)
def getTarget(city):
if city not in d:
return city
else:
return getTarget(d[city])
for s, t in paths:
t2 = getTarget(t)
if t2 is not None:
return t2
################################################################################
if __name__ == "__main__":
solution = Solution()
print(
solution.destCity(
[["London", "New York"], ["New York", "Lima"], ["Lima", "Sao Paulo"]]
)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33