151. Reverse Words in a String

来源:互联网 发布:淘宝代刷销量平台 编辑:程序博客网 时间:2024/04/28 15:55

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.

click to show clarification.

Subscribe to see which companies asked this question

分析:


代码:

class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        s=s.strip()
        if not s:
            return s
        s1=s.split(" ")
        b=[]
        for temp in s1:
            if temp!="":
                b.append(temp)
        b=b[::-1]
        s2=" ".join(b)
        return s2
       

0 0
原创粉丝点击