給定一個字符串,你需要反轉字符串中每個單詞的字符順序,同時仍保留空格和單詞的初始順序。
示例?1:
輸入: "Let's take LeetCode contest"
輸出: "s'teL ekat edoCteeL tsetnoc"?
注意:在字符串中,每個單詞由單個空格分隔,并且字符串中不會有任何額外的空格。
思路:python處理,先split分割為列表,再用列表生成式把每一項翻轉,再用join拼起來即可。
class Solution:def reverseWords(self, s: str) -> str:return " ".join(word[::-1] for word in s.split())
?