leetcode151

 1 class Solution:
 2     def reverseWords(self, s: str) -> str:
 3         s = s.strip()
 4         ary = s.split(' ')
 5         temp = []
 6         for string in ary:
 7             if len(string) != 0 and string != '':
 8                 temp.append(string)
 9         for i in range(len(temp)):
10             temp[i] = temp[i][::-1]#当前字符串反转
11         
12         return " ".join(temp)[::-1]#整个字符串反转

剑指Offer 58题相似,增加了对空白字符的处理。

原文地址:https://www.cnblogs.com/asenyang/p/12016030.html