From fc9a533b911cb4e422de25b261831fd01e613880 Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Mon, 11 Sep 2023 09:23:48 -0400 Subject: [PATCH] feat: add rust solution to lc problem: No.1282 --- .../README.md | 21 +++++++++++++++++++ .../README_EN.md | 21 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md index 3d17d1eee2adf..6af55770c9651 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md @@ -196,6 +196,27 @@ impl Solution { } ``` +```rust +impl Solution { + #[allow(dead_code)] + pub fn group_the_people(group_sizes: Vec) -> Vec> { + let n = group_sizes.len(); + let mut g = vec![vec![]; n + 1]; + let mut ret = vec![]; + + for i in 0..n { + g[group_sizes[i] as usize].push(i as i32); + if g[group_sizes[i] as usize].len() == group_sizes[i] as usize { + ret.push(g[group_sizes[i] as usize].clone()); + g[group_sizes[i] as usize].clear(); + } + } + + ret + } +} +``` + ### **...** ``` diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md index faac6b1b0e4fc..e91a93fb30c95 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md @@ -178,6 +178,27 @@ impl Solution { } ``` +```rust +impl Solution { + #[allow(dead_code)] + pub fn group_the_people(group_sizes: Vec) -> Vec> { + let n = group_sizes.len(); + let mut g = vec![vec![]; n + 1]; + let mut ret = vec![]; + + for i in 0..n { + g[group_sizes[i] as usize].push(i as i32); + if g[group_sizes[i] as usize].len() == group_sizes[i] as usize { + ret.push(g[group_sizes[i] as usize].clone()); + g[group_sizes[i] as usize].clear(); + } + } + + ret + } +} +``` + ### **...** ```