mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0332.重新安排行程 Rust版本
添加 0332.重新安排行程 Rust版本
This commit is contained in:
@ -742,5 +742,33 @@ 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>
|
<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