From 49a4df064ac27b39274dd15e91cfd75bd7ea5856 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 5 Sep 2024 11:05:36 +0800 Subject: [PATCH] 20240905 finished. --- src/problem/mod.rs | 3 +- src/problem/p3174_clear_digits.rs | 50 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/problem/p3174_clear_digits.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index eadaa66..88ea177 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -225,4 +225,5 @@ mod p3127_make_a_square_with_the_same_color; mod p1450_number_of_students_doing_homework_at_a_given_time; mod p2024_maximize_the_confusion_of_an_exam; mod p2708_maximum_strength_of_a_group; -mod p2860_happy_students; \ No newline at end of file +mod p2860_happy_students; +mod p3174_clear_digits; \ No newline at end of file diff --git a/src/problem/p3174_clear_digits.rs b/src/problem/p3174_clear_digits.rs new file mode 100644 index 0000000..ea0f55e --- /dev/null +++ b/src/problem/p3174_clear_digits.rs @@ -0,0 +1,50 @@ +/** + * [3174] Clear Digits + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn clear_digits(s: String) -> String { + let s: Vec = s.chars().collect(); + let mut mark = vec![true; s.len()]; + + for (i, &c) in s.iter().enumerate() { + if !c.is_ascii_digit() { + continue; + } + + // 是数字 + mark[i] = false; + let mut last = i - 1; + + while !mark[last] { + last -= 1; + } + mark[last] = false; + } + + s.iter().enumerate().filter_map(|(i, &c)| { + if mark[i] { + Some(c) + } else { + None + } + }).collect() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3174() { + assert_eq!("abc".to_owned(), Solution::clear_digits("abc".to_owned())); + assert_eq!("".to_owned(), Solution::clear_digits("cb34".to_owned())); + } +}