-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsets_ii.rs
68 lines (57 loc) · 1.72 KB
/
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
/// Given an integer array `nums` that may contain duplicates, return all possible subsets (the
/// power set).
///
/// The solution set must not contain duplicate subsets. Return the solution in any order.
struct Solution;
impl Solution {
fn backtrack(results: &mut Vec<Vec<i32>>, nums: &Vec<i32>, current: Vec<i32>, i: usize) {
let n = nums.len();
if i == n {
results.push(current);
} else {
let num = nums[i];
let mut cloned = current.clone();
cloned.push(num);
Self::backtrack(results, nums, cloned, i+1);
let mut j = i;
loop {
if j == n {
break;
} else if nums[j] == num {
j += 1;
} else {
break;
}
}
Self::backtrack(results, nums, current, j);
}
}
pub fn subsets_with_dup(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut nums = nums;
nums.sort();
let mut results = Vec::new();
let current = vec![];
Self::backtrack(&mut results, &nums, current, 0);
results
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let nums = vec![1,2,2];
let mut result = Solution::subsets_with_dup(nums);
result.sort();
let mut expected = vec![vec![], vec![1], vec![1,2], vec![1,2,2], vec![2], vec![2,2]];
expected.sort();
assert_eq!(result, expected);
}
#[test]
fn example_2() {
let nums = vec![0];
let mut result = Solution::subsets_with_dup(nums);
result.sort();
assert_eq!(result, vec![vec![], vec![0]]);
}
}