From ac4bdf21015d0bdcd72522b4190158b36deea391 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Tue, 19 Jul 2022 09:40:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200090.=E5=AD=90=E9=9B=86II?= =?UTF-8?q?=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0090.子集II Rust版本 --- problems/0090.子集II.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index e85ec66d..7ab6afb6 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -340,6 +340,36 @@ function subsetsWithDup(nums: number[]): number[][] { }; ``` +### Rust + +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, nums: &Vec, start_index: usize, used: &mut Vec) { + result.push(path.clone()); + let len = nums.len(); + // if start_index >= len { return; } + for i in start_index..len { + if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; } + path.push(nums[i]); + used[i] = true; + Self::backtracking(result, path, nums, i + 1, used); + used[i] = false; + path.pop(); + } + } + + pub fn subsets_with_dup(nums: Vec) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + let mut used = vec![false; nums.len()]; + let mut nums = nums; + nums.sort(); + Self::backtracking(&mut result, &mut path, &nums, 0, &mut used); + result + } +} +``` + ### C ```c