-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "lc-0078-subsets" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) 2023 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be | ||
// found in the LICENSE file. | ||
|
||
pub fn get_all<T: Copy>(out: &mut Vec<Vec<T>>, data: &[T], chunk_len: usize) { | ||
if chunk_len == 0 { | ||
out.push(Vec::new()); | ||
return; | ||
} | ||
if chunk_len == data.len() { | ||
out.push(data.to_vec()); | ||
return; | ||
} | ||
|
||
let len = data.len(); | ||
let min = 2_usize.pow(chunk_len as u32) - 1; | ||
let mut mask = 2_usize.pow(len as u32) - 2_usize.pow((len - chunk_len) as u32); | ||
|
||
fn get_chunk<T: Copy>(mask: usize, data: &[T]) -> Vec<T> { | ||
let b = format!("{:01$b}", mask, data.len()); | ||
b.chars() | ||
.enumerate() | ||
.filter(|&(_, e)| e == '1') | ||
.map(|(i, _)| data[i]) | ||
.collect() | ||
} | ||
|
||
while mask >= min { | ||
if mask.count_ones() as usize == chunk_len { | ||
let res = get_chunk(mask, data); | ||
mask -= 1; | ||
out.push(res); | ||
} else { | ||
mask -= 1; | ||
} | ||
} | ||
} | ||
|
||
// Combinations | ||
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> { | ||
let mut out = vec![]; | ||
for i in 0..=nums.len() { | ||
get_all(&mut out, &nums, i); | ||
} | ||
out | ||
} | ||
|
||
fn main() { | ||
let nums = vec![1, 2, 3]; | ||
let expected_out = vec![ | ||
vec![], | ||
vec![1], | ||
vec![2], | ||
vec![3], | ||
vec![1, 2], | ||
vec![1, 3], | ||
vec![2, 3], | ||
vec![1, 2, 3], | ||
]; | ||
assert_eq!(subsets(nums), expected_out); | ||
|
||
let nums = vec![0]; | ||
let expected_out = vec![vec![], vec![0]]; | ||
assert_eq!(subsets(nums), expected_out); | ||
} |