-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
39 lines (35 loc) · 794 Bytes
/
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
fn main() {
Solution::subsets(vec![1,2,3]);
}
struct Solution {}
impl Solution {
/// Recursion
pub fn subsets(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
if nums.len() == 0 { return vec![vec![]] }
let last = nums.pop().unwrap();
let remain_subsets = Self::subsets(nums);
let mut res = remain_subsets.clone();
for mut v in remain_subsets {
v.push(last);
res.push(v);
}
res
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::subsets(vec![1,2,3]), vec![
vec![3],
vec![1],
vec![2],
vec![1,2,3],
vec![1,3],
vec![2,3],
vec![1,2],
vec![]
]);
}
}