zoukankan      html  css  js  c++  java
  • LeetCode:151. 翻转字符串里的单词

    1、题目描述

    给定一个字符串,逐个翻转字符串中的每个单词。

    示例:  

    输入: "the sky is blue",
    输出: "blue is sky the".
    

    说明:

    • 无空格字符构成一个单词。
    • 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
    • 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。

    进阶: 请选用C语言的用户尝试使用 O(1) 空间复杂度的原地解法。

    2、题解

    2.1、解法一

    class Solution(object):
        def reverseWords(self, s):
            """
            :type s: str
            :rtype: str
            """
            if s is None:
                return ""
            s = s.strip().split(" ")
            new = [i for i in s if i != ""]
            if not new:
                return ""
            new.reverse()
            return " ".join(new)
    

      

  • 相关阅读:
    C++--第12课
    C++--第11课
    C++--第10课
    C++--第9课
    C++--第8课
    C++--第7课
    鼠标
    MessageBox函数
    Windows对应的"Hello,world"程序
    网络上有哪些免费的教育资源?
  • 原文地址:https://www.cnblogs.com/bad-robot/p/10065490.html
Copyright © 2011-2022 走看看