本题注意点:str.split(' ')的逆操作,其中join() 用于将序列中的元素以指定的字符连接生成一个字符串,strip()用于删除头尾空格。
import numpy as np
def reverseWords(s):
"""
:type s: str
:rtype: str
"""
words = s.split(' ')
reversed = []
for i in range(len(words)):
reversed.append(words[i][::-1])
return ' '.join(word for word in reversed).strip()
# sample:
s = "Let's take LeetCode contest"
print(reverseWords(s))