用空格分隔
逆向录入
注意 只能间隔一个空格
最后strip
或自己实现strip
class Solution:
def reverseWords(self, s: str) -> str:
x = s.split(' ')
print(x)
length = len(x)
res = ''
last = ''
for i in range(length-1,-1,-1):
if x[i] == ' ' or x[i] == '':
continue
else:
res += x[i]
res += ' '
start = 0
end = len(res)-1
while start <= end and res[0] == ' ':
start += 1
if start > end:
return s[start:start]
while res[end] == ' ':
end -= 1
return res[start:end+1]#.strip()