题目描述
1436. 旅行终点站
解法:(Python)
找出度为 0 的点
第一次打比赛的时候,我就按照邻接矩阵中规中矩的去找一个一行全为0的结点
class Solution:def destCity(self, paths: List[List[str]]) -> str:cities = []for path in paths:city1, city2 = path[0], path[1]if city1 not in cities:cities.append(city1)if city2 not in cities:cities.append(city2)graph = [[0 for _ in range(len(cities))] for _ in range(len(cities))]for path in paths:city1_indx, city2_indx = cities.index(path[0]), cities.index(path[1])graph[city1_indx][city2_indx] = 1for i in range(len(graph)):if all(j == 0 for j in graph[i]): return cities[i]
但是,今天写博客想了想,确实没必要,直接集合啊
class Solution:def destCity(self, paths: List[List[str]]) -> str:return (set(p[1] for p in paths) - set(p[0] for p in paths)).pop()