leetcode/src/problem/p151_reverse_words_in_a_string.rs
2024-10-24 09:08:13 +08:00

44 lines
813 B
Rust

/**
* [151] Reverse Words in a String
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn reverse_words(s: String) -> String {
let mut words = Vec::new();
for word in s.split(' ') {
if word.len() != 0 {
words.push(word);
}
}
let length = words.len();
let mut result = String::from(words[length - 1]);
for i in (0..length - 1).rev() {
result.push(' ');
result.push_str(words[i]);
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_151() {
assert_eq!(
"blue is sky the".to_owned(),
Solution::reverse_words("the sky is blue".to_owned())
);
}
}