20240905 finished.

This commit is contained in:
jackfiled 2024-09-05 11:05:36 +08:00
parent 12d3a89857
commit 49a4df064a
2 changed files with 52 additions and 1 deletions

View File

@ -226,3 +226,4 @@ mod p1450_number_of_students_doing_homework_at_a_given_time;
mod p2024_maximize_the_confusion_of_an_exam; mod p2024_maximize_the_confusion_of_an_exam;
mod p2708_maximum_strength_of_a_group; mod p2708_maximum_strength_of_a_group;
mod p2860_happy_students; mod p2860_happy_students;
mod p3174_clear_digits;

View File

@ -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<char> = 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()));
}
}