1436.旅行终点站
链接:1436.旅行终点站
难度:Easy
标签:数组、哈希表、字符串
简介:给你一份旅游线路图,该线路图中的旅行线路用数组 paths 表示,其中 paths[i] = [cityAi, cityBi] 表示该线路将会从 cityAi 直接前往 cityBi 。请你找出这次旅行的终点站,即没有任何可以通往其他城市的线路的城市。
题解 1 - python
- 编辑时间:2024-10-08
- 执行用时:43ms
- 内存消耗:16.3MB
- 编程语言:python
- 解法介绍:哈希存储。
class Solution:
    def destCity(self, paths: List[List[str]]) -> str:
        city_map = defaultdict(int)
        for a, b in paths:
            city_map[a] += 1
            city_map[b]
        return [city for city, arr in city_map.items() if not arr][0]
题解 2 - typescript
- 编辑时间:2021-10-01
- 执行用时:84ms
- 内存消耗:40.9MB
- 编程语言:typescript
- 解法介绍:哈希。
function destCity(paths: string[][]): string {
  const map = new Map<string, string>();
  const set = new Set<string>();
  for (const [c1, c2] of paths) {
    map.set(c1, c2);
    set.add(c1);
    set.add(c2);
  }
  for (const c of map.keys()) set.delete(c);
  return [...set][0];
}