557.反转字符串中的单词 III 提高效率

This commit is contained in:
游由 2021-11-16 17:42:56 +08:00
parent 94f76f5ffe
commit 1da014d08e
1 changed files with 2 additions and 1 deletions

View File

@ -20,7 +20,8 @@ impl Solution {
///
/// 👍 311 👎 0
pub fn reverse_words(s: String) -> String {
s.split(' ').into_iter().map(|a| a.chars().rev().collect::<String>()).collect::<Vec<String>>().join(" ")
s.split(' ').into_iter().map(|a| unsafe { String::from_utf8_unchecked(a.bytes().rev().collect::<Vec<u8>>()) }).collect::<Vec<String>>().join(" ")
//s.split_whitespace().into_iter().map(|a| a.chars().rev().collect::<String>()).collect::<Vec<String>>().join(" ")
}
}