20240313 Finished

This commit is contained in:
jackfiled 2024-03-13 10:08:32 +08:00
parent d6d6d5cea5
commit cc36203e43
2 changed files with 54 additions and 1 deletions

View File

@ -71,4 +71,5 @@ mod p2834_find_the_minimum_possible_sum_of_a_beautiful_array;
mod p2386_find_the_k_sum_of_an_array;
mod p299_bulls_and_cows;
mod p2129_capitalize_the_title;
mod p1261_find_elements_in_a_contaminated_binary_tree;
mod p1261_find_elements_in_a_contaminated_binary_tree;
mod p2864_maximum_odd_binary_number;

View File

@ -0,0 +1,52 @@
/**
* [2864] Maximum Odd Binary Number
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn maximum_odd_binary_number(s: String) -> String {
let nums: Vec<u32> = s.chars()
.map(|c| c.to_digit(10).unwrap())
.collect();
let mut one_count = 0;
let mut zero_count = 0;
for num in &nums {
if *num == 1 {
one_count += 1;
} else if *num == 0 {
zero_count += 1;
}
}
let mut result: Vec<char> = Vec::with_capacity(nums.len());
for _ in 1..one_count {
result.push('1');
}
for _ in 0..zero_count {
result.push('0');
}
result.push('1');
result.iter().collect()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2864() {
assert_eq!("001", Solution::maximum_odd_binary_number("010".to_owned()));
assert_eq!("1001", Solution::maximum_odd_binary_number("0101".to_owned()));
}
}