zoukankan      html  css  js  c++  java
  • 557. 反转字符串中的单词 III

    给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
    注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。

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

    思路详见注释。
     1 class Solution(object):
     2     def reverseWords(self, s):
     3         """
     4         :type s: str
     5         :rtype: str
     6         """
     7         # 每个单词由空格分隔,所以先按空格截取字符串
     8         s = s.split()
     9         # print(type(s), type(s[0]))
    10         # 遍历集合,将其中的每个字符串元素反转
    11         for i in range(len(s)):
    12             s[i] = s[i][::-1]
    13         # 取集合中的元素并用空格拼接,然后返回拼接好的字符串
    14         return ' '.join(s)
    15 
    16 if __name__ == '__main__':
    17     solution = Solution()
    18     print(solution.reverseWords("Let's take LeetCode contest"))
    19     print(solution.reverseWords("s'teL ekat edoCteeL tsetnoc"))
    
    
    
     
  • 相关阅读:
    [ZJOI2011]营救皮卡丘
    TJOI2018Party
    HEOI2013SAO
    [BJOI2017]树的难题
    [HNOI2016]序列
    [SHOI2007]善意的投票
    CF802C Heidi and Library (hard)
    SPOJ DIVCNT2
    LOJ子序列
    BZOJ2882工艺
  • 原文地址:https://www.cnblogs.com/panweiwei/p/12682270.html
Copyright © 2011-2022 走看看