题目:
给定一个字符串,逐个翻转字符串中的每个单词。
要求删除多余空格。
class Solution:
def reverseWords(self, s: str) -> str:
if len(s) == 0:
return ""
ss = s.split(" ")
res = ""
for word in ss:
if word != " " and word != "":
res = word + " " + res
if len(res) > 0 and res[-1] == " ":
res = res[:-1]
return res