mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
0332.重新安排行程 Javascript
This commit is contained in:
@ -399,6 +399,49 @@ char ** findItinerary(char *** tickets, int ticketsSize, int* ticketsColSize, in
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Javascript:
|
||||||
|
```Javascript
|
||||||
|
|
||||||
|
var findItinerary = function(tickets) {
|
||||||
|
let result = ['JFK']
|
||||||
|
let map = {}
|
||||||
|
|
||||||
|
for (const tickt of tickets) {
|
||||||
|
const [from, to] = tickt
|
||||||
|
if (!map[from]) {
|
||||||
|
map[from] = []
|
||||||
|
}
|
||||||
|
map[from].push(to)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const city in map) {
|
||||||
|
// 对到达城市列表排序
|
||||||
|
map[city].sort()
|
||||||
|
}
|
||||||
|
function backtracing() {
|
||||||
|
if (result.length === tickets.length + 1) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (!map[result[result.length - 1]] || !map[result[result.length - 1]].length) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for(let i = 0 ; i < map[result[result.length - 1]].length; i++) {
|
||||||
|
let city = map[result[result.length - 1]][i]
|
||||||
|
// 删除已走过航线,防止死循环
|
||||||
|
map[result[result.length - 1]].splice(i, 1)
|
||||||
|
result.push(city)
|
||||||
|
if (backtracing()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
result.pop()
|
||||||
|
map[result[result.length - 1]].splice(i, 0, city)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
backtracing()
|
||||||
|
return result
|
||||||
|
};
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
Reference in New Issue
Block a user