题目链接:https://leetcode.com/problems/reverse-words-in-a-string-iii/
代码
class Solution:
def reverseWords(self, s: str) -> str:
n=len(s)
start=0
res=''
i=0
while i < n :
while i<n and s[i] != ' ':
i+=1
res+=s[start:i][::-1]+' '
i+=1
start=i
return res[:-1]
class Solution:
def reverseWords(self, s: str) -> str:
return ' '.join([word[::-1] for word in s.split(' ')])