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题相似,增加了对空白字符的处理。