20240331 Finished
This commit is contained in:
parent
2635eeb818
commit
e839fe55e2
|
@ -89,4 +89,5 @@ mod p2642_design_graph_with_shortest_path_calculator;
|
|||
mod p2580_count_ways_to_group_overlapping_ranges;
|
||||
mod p1997_first_day_where_you_have_been_in_all_the_rooms;
|
||||
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;
|
|
@ -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")));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user