forked from aylei/leetcode-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn0090_subsets_ii.rs
94 lines (87 loc) · 2.07 KB
/
n0090_subsets_ii.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* [90] Subsets II
*
* Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
*
* Note: The solution set must not contain duplicate subsets.
*
* Example:
*
*
* Input: [1,2,2]
* Output:
* [
* [2],
* [1],
* [1,2,2],
* [2,2],
* [1,2],
* []
* ]
*
*
*/
pub struct Solution {}
// submission codes start here
/*
count the repeats of each number,
then in backtracking, each number can be picked up for 0..repeat times
using BTreeMap to preserve order (easy for test)
*/
use std::collections::BTreeMap;
impl Solution {
pub fn subsets_with_dup(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut res = Vec::new();
let nums = nums.into_iter()
.fold(BTreeMap::new(), |mut map, v| {
*map.entry(v).or_insert(0) += 1; map
}).into_iter().collect::<Vec<(i32,i32)>>();
Solution::backtrack(0, vec![], &nums, &mut res);
res
}
fn backtrack(start: usize, mut curr: Vec<i32>, nums: &Vec<(i32, i32)>, result: &mut Vec<Vec<i32>>) {
if start >= nums.len() {
result.push(curr);
return
}
for repeat in 0..nums[start].1+1 {
let mut inner = curr.clone();
for _ in 0..repeat {
inner.push(nums[start].0);
}
Solution::backtrack(start+1, inner, nums, result);
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_90() {
assert_eq!(
Solution::subsets_with_dup(vec![1,2,2]),
vec![
vec![],
vec![2],
vec![2,2],
vec![1],
vec![1,2],
vec![1,2,2],
]
);
assert_eq!(
Solution::subsets_with_dup(vec![1]),
vec![
vec![],
vec![1],
]
);
assert_eq!(
Solution::subsets_with_dup(vec![]),
vec![
vec![],
]
);
}
}