[LeetCode]557. Reverse Words in a String III (Easy) Python-2023.10.01
Posted on 2023-10-01 17:00 Uriel 閱讀(49) 評論(0) 編輯 收藏 引用 所屬分類: 字符串處理 、閑來無事重切Leet Code翻轉一個字符串中的每個單詞,python很好實現,用空格分割然后各自翻轉
1 #557
2 #Runtime: 14 ms (Beats 91.96%)
3 #Memory: 14.2 MB (Beats 88.75%)
4
5 class Solution(object):
6 def reverseWords(self, s):
7 """
8 :type s: str
9 :rtype: str
10 """
11 return ' '.join(s.split( )[::-1])[::-1]
2 #Runtime: 14 ms (Beats 91.96%)
3 #Memory: 14.2 MB (Beats 88.75%)
4
5 class Solution(object):
6 def reverseWords(self, s):
7 """
8 :type s: str
9 :rtype: str
10 """
11 return ' '.join(s.split( )[::-1])[::-1]