Skip to content

docs: more concise rust code in 根据身高重建队列(vector原理讲解).md #2888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update 根据身高重建队列(vector原理讲解).md
Concise rust implementation
  • Loading branch information
young010101 authored Feb 10, 2025
commit 6b3717fe413b824d78e1ab0f3af5d509022e38d5
29 changes: 10 additions & 19 deletions problems/根据身高重建队列(vector原理讲解).md
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,17 @@ public:
```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());
}
impl Solution {
pub fn reconstruct_queue(people: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut people = people;
people.sort_by_key(|b| (-b[0], b[1]));
let mut res = LinkedList::new();
for person in people {
let mut backlink = res.split_off(person[1] as usize);
res.push_back(person);
res.append(&mut backlink);
}
queue.into_iter().collect()
res.into_iter().collect()
}
}
```
Expand Down