1173. 反转字符串 III

1173. 反转字符串 III

中文English

给定一个字符串句子,反转句子中每一个单词的所有字母,同时保持空格和最初的单词顺序。

样例

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

注意事项

字符串中,每一个单词都是由空格隔开,字符串中不会有多余的空格。

class Solution:
    """
    @param s: a string
    @return: reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order
    """
    '''
    大致思路:
    1.将字符串切割,以空格的方式切割成列表
    2.循环该列表,初始化res = '',反转字符串,以空格加到res里面.
    '''
    def reverseWords(self,s):
        res = ''
        dic = s.split(' ')
        for i in range(len(dic)-1):
            res += dic[i][::-1] + ' '
        return res + dic[-1][::-1]
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12725008.html