测试写的:
s = input()
lst = s.strip().split(" ")
s1 = []
for i in lst:
if i != '':
s1.append(i)
s1.reverse()
print(" ".join(s1))
输入:a good example!
输出:example! good a
leetcode 上提交的:
class Solution:
def reverseWords(self, s: str) -> str:
lst = s.strip().split(" ") # s.strip()是为了去掉字符串前后的空格;split(" ")是以空格进行分隔
s1 = []
for i in lst:
if i != '':
s1.append(i)
s1.reverse()
return (" ".join(s1))