zoukankan      html  css  js  c++  java
  • 1.(字符串)-反转单词

    如给定:"the sky is blue",
    返回:  "blue is sky the".(多个空格变成一个空格)

    python:

    class Solution:
        def str(self, s):
            if len(s) == 0:
                return ''
            temp = []
            i = 0
            while i < len(s):
                j = i
                while not s[j].isspace():#没有遇到单词之间的空格时,一直往下+
                    j += 1
                    if j == len(s):#遇到末尾时也停止
                        break
                if j != i:#如果不相等,说明存在新单词划分
                    temp.append(s[i:j])#添加新单词
                i = j + 1    #第j位是空格,从j+1位开始继续判断
    
            i = len(temp) - 1
            s = ''
            while i > 0:
                s += temp[i]
                s += ' '
                i -= 1
            s += temp[i]#最后末尾不需要空格
            return s
    
    
    s = '     the     sky    is   blue  '
    sou = Solution()
    print(sou.str(s))
    #blue is sky the
  • 相关阅读:
    Codeforces 798C
    Codeforces 798B
    Codeforces 798A
    HDU
    HDU
    HDU
    加速cin的技巧
    Codeforces Gym
    Codeforces Gym
    Solutions to an Equation LightOJ
  • 原文地址:https://www.cnblogs.com/onenoteone/p/12441789.html
Copyright © 2011-2022 走看看