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]

 

相关文章:

  • 2022-01-04
  • 2021-08-16
  • 2021-08-15
  • 2021-07-19
猜你喜欢
  • 2022-01-26
  • 2021-11-21
  • 2021-07-10
  • 2022-12-23
相关资源
相似解决方案