@@ -440,7 +440,7 @@ impl Solution {
440
440
let len = nums . len ();
441
441
// if start_index >= len { return; }
442
442
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 ; }
444
444
path . push (nums [i ]);
445
445
used [i ] = true ;
446
446
Self :: backtracking (result , path , nums , i + 1 , used );
@@ -449,18 +449,46 @@ impl Solution {
449
449
}
450
450
}
451
451
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 >> {
453
453
let mut result : Vec <Vec <i32 >> = Vec :: new ();
454
454
let mut path : Vec <i32 > = Vec :: new ();
455
455
let mut used = vec! [false ; nums . len ()];
456
- let mut nums = nums ;
457
456
nums . sort ();
458
457
Self :: backtracking (& mut result , & mut path , & nums , 0 , & mut used );
459
458
result
460
459
}
461
460
}
462
461
```
463
462
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
+
464
492
### C
465
493
466
494
``` c
0 commit comments