20241029 finished.

This commit is contained in:
jackfiled 2024-10-29 11:11:33 +08:00
parent d7e433f502
commit f0ba102a70
2 changed files with 51 additions and 0 deletions

View File

@ -282,3 +282,5 @@ mod p3181_maximum_total_reward_using_operations_ii;
mod p684_redundant_connection;
mod p685_redundant_connection_ii;
mod p3211_generate_binary_strings_without_adjacent_zeros;

View File

@ -0,0 +1,49 @@
/**
* [3211] Generate Binary Strings Without Adjacent Zeros
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn valid_strings(n: i32) -> Vec<String> {
let n = n as usize;
let mut result = Vec::new();
let mut str = vec!['0'; n];
Self::dfs(0, false, &mut str, &mut result);
result
}
fn dfs(i: usize, last_zero: bool, str: &mut Vec<char>, result: &mut Vec<String>) {
if i >= str.len() {
result.push(str.iter().collect());
return;
}
if !last_zero {
str[i] = '0';
Self::dfs(i + 1, true, str, result);
}
str[i] = '1';
Self::dfs(i + 1, false, str, result);
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3211() {
assert_eq!(
vec_string!("010", "011", "101", "110", "111"),
Solution::valid_strings(3)
);
assert_eq!(vec_string!("0", "1"), Solution::valid_strings(1));
}
}