Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1987 from fwqaaq/patch-24
Browse files Browse the repository at this point in the history
Update 根据身高重建队列(vector原理讲解).md about rust
  • Loading branch information
youngyangyang04 authored Apr 9, 2023
2 parents 5f20987 + f6a2286 commit ba03a17
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions problems/根据身高重建队列(vector原理讲解).md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,35 @@ Java:
Python:
Rust:
```rust
// 版本二,使用list(链表)
use std::collections::LinkedList;
impl Solution{
pub fn reconstruct_queue(mut people: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut queue = LinkedList::new();
people.sort_by(|a, b| {
if a[0] == b[0] {
return a[1].cmp(&b[1]);
}
b[0].cmp(&a[0])
});
queue.push_back(people[0].clone());
for v in people.iter().skip(1) {
if queue.len() > v[1] as usize {
let mut back_link = queue.split_off(v[1] as usize);
queue.push_back(v.clone());
queue.append(&mut back_link);
} else {
queue.push_back(v.clone());
}
}
queue.into_iter().collect()
}
}
```


Go:

Expand Down

0 comments on commit ba03a17

Please sign in to comment.