-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
57 lines (53 loc) · 1.2 KB
/
main.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
fn main() {
let mut a = vec![1,2,3];
for i in 0..a.len() {
println!("a = {:#?}", a);
a.push(i);
}
assert_eq!(Solution::subsets_with_dup(vec![1,2,2]), vec![
vec![2],
vec![1],
vec![1,2,2],
vec![2,2],
vec![1,2],
vec![]
]);
}
struct Solution {}
impl Solution {
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut res = vec![vec![]];
nums.sort();
let mut i = 0;
while i < nums.len() {
let mut cnt = 0;
while i + cnt < nums.len() && nums[i + cnt] == nums[i] {
cnt += 1;
}
for j in 0..res.len() {
let mut a = res[j].clone();
for _ in 0..cnt {
a.push(nums[i]);
res.push(a.clone());
}
}
i += cnt;
}
res
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::subsets_with_dup(vec![1,2,2]), vec![
vec![2],
vec![1],
vec![1,2,2],
vec![2,2],
vec![1,2],
vec![]
]);
}
}