class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
reverse = []
sentence = s.split()
for sent in sentence:
reverse.append(sent[::-1])
return " ".join(reverse)
leetcode - 557 - 反转字符串中的单词 III
最新推荐文章于 2020-08-31 13:26:14 发布
本文介绍了一种使用Python实现字符串中每个单词反转的方法。通过split()函数分割句子为单词列表,然后利用切片[::-1]反转每个单词,最后用.join()将所有单词连接成新的字符串。
539

被折叠的 条评论
为什么被折叠?



