Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
|
||||
# 332.重新安排行程
|
||||
|
||||
[力扣题目链接](https://leetcode-cn.com/problems/reconstruct-itinerary/)
|
||||
[力扣题目链接](https://leetcode.cn/problems/reconstruct-itinerary/)
|
||||
|
||||
给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从 JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 开始。
|
||||
|
||||
@@ -379,7 +379,7 @@ class Solution:
|
||||
return path
|
||||
```
|
||||
|
||||
### Go
|
||||
### GO
|
||||
```go
|
||||
type pair struct {
|
||||
target string
|
||||
@@ -543,6 +543,50 @@ var findItinerary = function(tickets) {
|
||||
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
function findItinerary(tickets: string[][]): string[] {
|
||||
/**
|
||||
TicketsMap 实例:
|
||||
{ NRT: Map(1) { 'JFK' => 1 }, JFK: Map(2) { 'KUL' => 1, 'NRT' => 1 } }
|
||||
这里选择Map数据结构的原因是:与Object类型的一个主要差异是,Map实例会维护键值对的插入顺序。
|
||||
*/
|
||||
type TicketsMap = {
|
||||
[index: string]: Map<string, number>
|
||||
};
|
||||
tickets.sort((a, b) => {
|
||||
return a[1] < b[1] ? -1 : 1;
|
||||
});
|
||||
const ticketMap: TicketsMap = {};
|
||||
for (const [from, to] of tickets) {
|
||||
if (ticketMap[from] === undefined) {
|
||||
ticketMap[from] = new Map();
|
||||
}
|
||||
ticketMap[from].set(to, (ticketMap[from].get(to) || 0) + 1);
|
||||
}
|
||||
const resRoute = ['JFK'];
|
||||
backTracking(tickets.length, ticketMap, resRoute);
|
||||
return resRoute;
|
||||
function backTracking(ticketNum: number, ticketMap: TicketsMap, route: string[]): boolean {
|
||||
if (route.length === ticketNum + 1) return true;
|
||||
const targetMap = ticketMap[route[route.length - 1]];
|
||||
if (targetMap !== undefined) {
|
||||
for (const [to, count] of targetMap.entries()) {
|
||||
if (count > 0) {
|
||||
route.push(to);
|
||||
targetMap.set(to, count - 1);
|
||||
if (backTracking(ticketNum, ticketMap, route) === true) return true;
|
||||
targetMap.set(to, count);
|
||||
route.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### Swift
|
||||
|
||||
直接迭代tickets数组:
|
||||
@@ -663,5 +707,105 @@ for line in tickets {
|
||||
}
|
||||
```
|
||||
|
||||
### Go
|
||||
```Go
|
||||
|
||||
// 先排序,然后找到第一条路径即可返回
|
||||
func findItinerary(tickets [][]string) []string {
|
||||
var path []string // 用来保存搜索的路径
|
||||
data := make(map[string]ticketSlice) // 用来保存tickets排序后的结果
|
||||
|
||||
var search func(airport string) bool
|
||||
search = func(airport string) bool {
|
||||
if len(path) == len(tickets) {
|
||||
path = append(path, airport)
|
||||
return true
|
||||
}
|
||||
to := data[airport]
|
||||
for _, item := range to {
|
||||
if item.Count == 0 {
|
||||
// 已用完
|
||||
continue
|
||||
}
|
||||
|
||||
path = append(path, airport)
|
||||
item.Count--
|
||||
if search(item.To) { return true }
|
||||
item.Count++
|
||||
path = path[:len(path) - 1]
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 排序
|
||||
// 感觉这段代码有点啰嗦,不知道能不能简化一下
|
||||
tmp := make(map[string]map[string]int)
|
||||
for _, ticket := range tickets {
|
||||
if to, ok := tmp[ticket[0]]; ok {
|
||||
if _, ok2 := to[ticket[1]]; ok2 {
|
||||
to[ticket[1]]++
|
||||
} else {
|
||||
to[ticket[1]] = 1
|
||||
}
|
||||
} else {
|
||||
tmp[ticket[0]] = map[string]int{
|
||||
ticket[1]: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
for from, to := range tmp {
|
||||
var tmp ticketSlice
|
||||
for to, num := range to {
|
||||
tmp = append(tmp, &ticketStat{To: to, Count: num})
|
||||
}
|
||||
sort.Sort(tmp)
|
||||
data[from] = tmp
|
||||
}
|
||||
|
||||
search("JFK")
|
||||
return path
|
||||
}
|
||||
|
||||
type ticketStat struct {
|
||||
To string
|
||||
Count int
|
||||
}
|
||||
type ticketSlice []*ticketStat
|
||||
|
||||
func (p ticketSlice) Len() int { return len(p) }
|
||||
func (p ticketSlice) Less(i, j int) bool { return strings.Compare(p[i].To, p[j].To) == -1 }
|
||||
func (p ticketSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
```
|
||||
|
||||
### Rust
|
||||
** 文中的Hashmap嵌套Hashmap的方法因为Rust的所有权问题暂时无法实现,此方法为删除哈希表中元素法 **
|
||||
```Rust
|
||||
use std::collections::HashMap;
|
||||
impl Solution {
|
||||
fn backtracking(airport: String, targets: &mut HashMap<&String, Vec<&String>>, result: &mut Vec<String>) {
|
||||
while let Some(next_airport) = targets.get_mut(&airport).unwrap_or(&mut vec![]).pop() {
|
||||
Self::backtracking(next_airport.clone(), targets, result);
|
||||
}
|
||||
result.push(airport.clone());
|
||||
}
|
||||
|
||||
pub fn find_itinerary(tickets: Vec<Vec<String>>) -> Vec<String> {
|
||||
let mut targets: HashMap<&String, Vec<&String>> = HashMap::new();
|
||||
let mut result = Vec::new();
|
||||
for t in 0..tickets.len() {
|
||||
targets.entry(&tickets[t][0]).or_default().push(&tickets[t][1]);
|
||||
}
|
||||
for (_, target) in targets.iter_mut() {
|
||||
target.sort_by(|a, b| b.cmp(a));
|
||||
}
|
||||
Self::backtracking("JFK".to_string(), &mut targets, &mut result);
|
||||
result.reverse();
|
||||
result
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
||||
Reference in New Issue
Block a user