From 77aef4b18badbd6e04e0d78d834af7fea2613027 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Sun, 5 Mar 2023 10:19:41 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E5=9B=9E=E6=BA=AF=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84=E5=8F=A6=E4=B8=80?= =?UTF-8?q?=E7=A7=8D=E5=86=99=E6=B3=95.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...溯算法去重问题的另一种写法.md | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md index 73862156..2265a89b 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.md @@ -525,10 +525,41 @@ function permuteUnique(nums: number[]): number[][] { }; ``` -Go: +Rust: +**90.子集II**: +```rust +use std::collections::HashSet; +impl Solution { + pub fn subsets_with_dup(mut nums: Vec) -> Vec> { + let mut res = vec![]; + let mut path = vec![]; + nums.sort(); + Self::backtracking(&nums, &mut path, &mut res, 0); + res + } + pub fn backtracking( + nums: &Vec, + path: &mut Vec, + res: &mut Vec>, + start_index: usize, + ) { + res.push(path.clone()); + let mut helper_set = HashSet::new(); + for i in start_index..nums.len() { + if helper_set.contains(&nums[i]) { + continue; + } + helper_set.insert(nums[i]); + path.push(nums[i]); + Self::backtracking(nums, path, res, i + 1); + path.pop(); + } + } +} +```