classSolution:defreverseWords(self, s:str)->str:
s = s.strip()# 删除首尾空格
i = j =len(s)-1
res =[]while i >=0:while i >=0and s[i]!=' ': i -=1# 搜索首个空格
res.append(s[i +1: j +1])# 添加单词while i >=0and s[i]==' ': i -=1# 跳过单词间空格
j = i # j 指向下个单词的尾字符return' '.join(res)# 拼接并返回