首页 > 其他 > 详细

1173. 反转字符串 III

时间:2020-04-18 14:14:15      阅读:55      评论:0      收藏:0      [点我收藏+]

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]

 

1173. 反转字符串 III

原文:https://www.cnblogs.com/yunxintryyoubest/p/12725008.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!