Skip to content

Commit 97e5867

Browse files
authored
Update 0090.子集II.md
1 parent c6bc3fc commit 97e5867

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

problems/0090.子集II.md

+31-3
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl Solution {
440440
let len = nums.len();
441441
// if start_index >= len { return; }
442442
for i in start_index..len {
443-
if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; }
443+
if i > 0 && nums[i] == nums[i - 1] && !used[i - 1] { continue; }
444444
path.push(nums[i]);
445445
used[i] = true;
446446
Self::backtracking(result, path, nums, i + 1, used);
@@ -449,18 +449,46 @@ impl Solution {
449449
}
450450
}
451451

452-
pub fn subsets_with_dup(nums: Vec<i32>) -> Vec<Vec<i32>> {
452+
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
453453
let mut result: Vec<Vec<i32>> = Vec::new();
454454
let mut path: Vec<i32> = Vec::new();
455455
let mut used = vec![false; nums.len()];
456-
let mut nums = nums;
457456
nums.sort();
458457
Self::backtracking(&mut result, &mut path, &nums, 0, &mut used);
459458
result
460459
}
461460
}
462461
```
463462

463+
set 去重版本
464+
465+
```rust
466+
use std::collections::HashSet;
467+
impl Solution {
468+
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
469+
let mut res = HashSet::new();
470+
let mut path = vec![];
471+
nums.sort();
472+
Self::backtracking(&nums, &mut path, &mut res, 0);
473+
res.into_iter().collect()
474+
}
475+
476+
pub fn backtracking(
477+
nums: &Vec<i32>,
478+
path: &mut Vec<i32>,
479+
res: &mut HashSet<Vec<i32>>,
480+
start_index: usize,
481+
) {
482+
res.insert(path.clone());
483+
for i in start_index..nums.len() {
484+
path.push(nums[i]);
485+
Self::backtracking(nums, path, res, i + 1);
486+
path.pop();
487+
}
488+
}
489+
}
490+
```
491+
464492
### C
465493

466494
```c

0 commit comments

Comments
 (0)