20240331 Finished
This commit is contained in:
		@@ -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 p2908_minimum_sum_of_mountain_triplets_i;
 | 
			
		||||
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")));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user