美文网首页
leetcode 557 python 反转字符串中的单词III

leetcode 557 python 反转字符串中的单词III

作者: 慧鑫coming | 来源:发表于2019-02-01 05:42 被阅读0次

传送门

题目要求

给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。

示例 1:
输入: "Let's take LeetCode contest"
输出: "s'teL ekat edoCteeL tsetnoc"

注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。

思路一

以空格分割字符串为列表,将列表中每个元素反转,以空格拼接列表中的元素

→_→ talk is cheap, show me the code

class Solution:
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        l = s.split(" ")
        for i in range(0, len(l)):
            l[i] = l[i][::-1]
        return " ".join(l)

相关文章

网友评论

      本文标题:leetcode 557 python 反转字符串中的单词III

      本文链接:https://www.haomeiwen.com/subject/rwxlsqtx.html