Update 0090.子集II.md

This commit is contained in:
fw_qaq
2023-02-22 22:15:19 +08:00
committed by GitHub
parent c6bc3fc678
commit 97e5867fac

View File

@ -440,7 +440,7 @@ impl Solution {
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; }
if i > 0 && nums[i] == nums[i - 1] && !used[i - 1] { continue; }
path.push(nums[i]);
used[i] = true;
Self::backtracking(result, path, nums, i + 1, used);
@ -449,11 +449,10 @@ impl Solution {
}
}
pub fn subsets_with_dup(nums: Vec<i32>) -> Vec<Vec<i32>> {
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new();
let mut path: Vec<i32> = 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
@ -461,6 +460,35 @@ impl Solution {
}
```
set 去重版本
```rust
use std::collections::HashSet;
impl Solution {
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut res = HashSet::new();
let mut path = vec![];
nums.sort();
Self::backtracking(&nums, &mut path, &mut res, 0);
res.into_iter().collect()
}
pub fn backtracking(
nums: &Vec<i32>,
path: &mut Vec<i32>,
res: &mut HashSet<Vec<i32>>,
start_index: usize,
) {
res.insert(path.clone());
for i in start_index..nums.len() {
path.push(nums[i]);
Self::backtracking(nums, path, res, i + 1);
path.pop();
}
}
}
```
### C
```c