20240331 Finished

This commit is contained in:
jackfiled 2024-03-31 11:46:39 +08:00
parent 2635eeb818
commit e839fe55e2
2 changed files with 53 additions and 1 deletions

View File

@ -90,3 +90,4 @@ mod p2580_count_ways_to_group_overlapping_ranges;
mod p1997_first_day_where_you_have_been_in_all_the_rooms; mod p1997_first_day_where_you_have_been_in_all_the_rooms;
mod p2908_minimum_sum_of_mountain_triplets_i; mod p2908_minimum_sum_of_mountain_triplets_i;
mod p2952_minimum_number_of_coins_to_be_added; mod p2952_minimum_number_of_coins_to_be_added;
mod p331_verify_preorder_serialization_of_a_binary_tree;

View File

@ -0,0 +1,51 @@
/**
* [331] Verify Preorder Serialization of a Binary Tree
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn is_valid_serialization(preorder: String) -> bool {
let mut slots = 1;
let mut i = preorder.chars();
while let Some(c) = i.next() {
if slots == 0 {
return false;
}
if (c == ',') {
continue;
}
if (c == '#') {
slots -= 1;
} else {
while let Some(c) = i.next() {
if c == ',' {
break;
}
}
slots += 1;
}
}
slots == 0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_331() {
assert!(Solution::is_valid_serialization(String::from("9,3,4,#,#,1,#,#,2,#,6,#,#")));
assert!(!Solution::is_valid_serialization(String::from("1,#")));
assert!(!Solution::is_valid_serialization(String::from("9,#,#,1")));
}
}