20250101 finished.

This commit is contained in:
jackfiled 2025-01-01 15:53:34 +08:00
parent 2aa12ebd2d
commit 4897c3b81d
2 changed files with 51 additions and 0 deletions

View File

@ -404,3 +404,5 @@ mod p1366_rank_teams_by_votes;
mod p1367_linked_list_in_binary_tree;
mod p3219_minimum_cost_for_cutting_cake_ii;
mod p3280_convert_date_to_binary;

View File

@ -0,0 +1,49 @@
/**
* [3280] Convert Date to Binary
*/
pub struct Solution {}
// submission codes start here
use std::str::FromStr;
impl Solution {
pub fn convert_date_to_binary(date: String) -> String {
date.split('-')
.map(|x| u32::from_str(x).unwrap())
.map(|mut x| {
let mut result = vec![];
while x != 0 {
result.push(x % 2);
x = x / 2;
}
result
.iter()
.rev()
.map(|c| c.to_string())
.collect::<String>()
})
.reduce(|acc, e| acc + "-" + e.as_str())
.unwrap()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3280() {
assert_eq!(
"100000100000-10-11101".to_owned(),
Solution::convert_date_to_binary("2080-02-29".to_owned())
);
assert_eq!(
"11101101100-1-1".to_owned(),
Solution::convert_date_to_binary("1900-01-01".to_owned())
);
}
}