20240420 Finished

This commit is contained in:
jackfiled 2024-04-20 10:50:10 +08:00
parent 18e7167ac9
commit f84124d26c
2 changed files with 37 additions and 1 deletions

View File

@ -107,3 +107,4 @@ mod p238_product_of_array_except_self;
mod p134_gas_station; mod p134_gas_station;
mod p135_candy; mod p135_candy;
mod p42_trapping_rain_water; mod p42_trapping_rain_water;
mod p58_length_of_last_word;

View File

@ -0,0 +1,35 @@
/**
* [58] Length of Last Word
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn length_of_last_word(s: String) -> i32 {
let words : Vec<&str> = s.split(' ').collect();
for word in words.iter().rev() {
if word.len() != 0 {
return word.len() as i32;
}
}
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_58() {
assert_eq!(5, Solution::length_of_last_word("Hello World".to_owned()));
assert_eq!(4, Solution::length_of_last_word(" fly me to the moon ".to_owned()));
assert_eq!(6, Solution::length_of_last_word("luffy is still joyboy".to_owned()));
}
}