Leetcode 557. Reverse Words in a String III

Posted 周洋的Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 557. Reverse Words in a String III相关的知识,希望对你有一定的参考价值。

class Solution:
    def reverseWords(self, s: str) -> str:
        s = s.split()
        ans = ‘‘
        for i in s:
            ans += ‘‘.join(list(reversed(i))) +  
        return ans.strip()

优化:字符串的反转可以用切片来完成:string[::-1]

class Solution:
    def reverseWords(self, s: str) -> str:
        return  .join([word[::-1] for word in s.split()])

 

以上是关于Leetcode 557. Reverse Words in a String III的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode - 557. Reverse Words in a String III

LeetCode 557. Reverse Words in a String III

leetcode-557-Reverse Words in a String III

LeetCode 557. Reverse Words in a String III

Leetcode557 Reverse Words in a String III Java实现

leetcode No557. Reverse Words in a String III