From f6a22863841064c9f5c61ad606fa10f293494de9 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Wed, 29 Mar 2023 11:52:19 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=A0=B9=E6=8D=AE=E8=BA=AB=E9=AB=98?= =?UTF-8?q?=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97=EF=BC=88vector=E5=8E=9F?= =?UTF-8?q?=E7=90=86=E8=AE=B2=E8=A7=A3=EF=BC=89.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...高重建队列(vector原理讲解).md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/根据身高重建队列(vector原理讲解).md b/problems/根据身高重建队列(vector原理讲解).md index 94d94e38..4f8cab82 100644 --- a/problems/根据身高重建队列(vector原理讲解).md +++ b/problems/根据身高重建队列(vector原理讲解).md @@ -177,6 +177,35 @@ Java: Python: +Rust: + +```rust +// 版本二,使用list(链表) +use std::collections::LinkedList; +impl Solution{ + pub fn reconstruct_queue(mut people: Vec>) -> Vec> { + 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: