-
Notifications
You must be signed in to change notification settings - Fork 2
/
1436.旅行终点站.html
74 lines (63 loc) · 2.3 KB
/
1436.旅行终点站.html
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>1436. 旅行终点站</title>
</head>
<body>
<script>
// https://leetcode.cn/problems/destination-city/
// 给你一份旅游线路图,该线路图中的旅行线路用数组 paths 表示,其中 paths[i] = [cityAi, cityBi] 表示该线路将会从 cityAi 直接前往 cityBi 。请你找出这次旅行的终点站,即没有任何可以通往其他城市的线路的城市。
// 题目数据保证线路图会形成一条不存在循环的线路,因此恰有一个旅行终点站。
// 提示:
// 1 <= paths.length <= 100
// paths[i].length == 2
// 1 <= cityAi.length, cityBi.length <= 10
// cityAi != cityBi
// 所有字符串均由大小写英文字母和空格字符组成。
/**
* @param {string[][]} paths
* @return {string}
*/
var destCity = function (paths) {};
// --- answer-1 ---
// 题目中说明只有一条线路 那么形如列表我们从任一节点出发 直到尾结点
var destCity = function (paths = [[]]) {
// 使用map构建链
const map = paths.reduce((map, cur) => {
map.set(cur[0], cur[1]);
return map;
}, new Map());
let start = paths[0][0];
while (map.has(start)) {
start = map.get(start);
}
return start;
};
// --- answer-1 ---
// --- answer-2 ---
// --- answer-2 ---
var paths = [
['London', 'New York'],
['New York', 'Lima'],
['Lima', 'Sao Paulo']
];
var result = 'Sao Paulo';
// 解释:从 "London" 出发,最后抵达终点站 "Sao Paulo" 。本次旅行的路线是 "London" -> "New York" -> "Lima" -> "Sao Paulo" 。
var paths = [
['B', 'C'],
['D', 'B'],
['C', 'A']
];
var result = 'A';
// 解释:所有可能的线路是:
var paths = [['A', 'Z']];
var result = 'Z';
console.log('paths = ', paths);
console.log('result = ', result);
console.log('destCity = ', destCity(paths));
</script>
</body>
</html>