-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsets.rs
61 lines (54 loc) · 1.33 KB
/
subsets.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
#![allow(dead_code)]
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
fn backtrack(
nums: &Vec<i32>,
start: usize,
end: usize,
path: &mut Vec<i32>,
result: &mut Vec<Vec<i32>>,
) {
result.push(path.clone());
for i in start..end {
path.push(nums[i]);
backtrack(nums, i + 1, end, path, result);
path.pop();
}
}
let mut result = Vec::new();
backtrack(&nums, 0, nums.len(), &mut Vec::new(), &mut result);
result
}
/*
Algorithm - Backtracking
- Start with an empty set
- Add each element to the set
- Add the set to the result
- Remove the element from the set
- Repeat for each element
Time Complexity: O(n * 2^n)
Space Complexity: O(n * 2^n)
*/
#[cfg(test)]
mod tests {
use super::*;
fn validate_subsets(mut result: Vec<Vec<i32>>, expected: Vec<Vec<i32>>) -> bool {
result.sort();
result == expected
}
#[test]
fn test_subsets() {
assert!(validate_subsets(
subsets(vec![1, 2, 3]),
vec![
vec![],
vec![1],
vec![1, 2],
vec![1, 2, 3],
vec![1, 3],
vec![2],
vec![2, 3],
vec![3]
]
));
}
}