Reverse Words in a String

来源:互联网 发布:网络上市公司龙头股票 编辑:程序博客网 时间:2024/06/12 22:22

题目:

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.

解题思路:

此题比较简单,一次便AC。直接上代码

class Solution:
    # @param s, a string
    # @return a string
    def reverseWords(self, s):
        ans =''
        l = s.split()
        l.reverse()
        for index,val in enumerate(l):
            if index !=len(l)-1:
                ans += val+' '
            else:
                ans += val
        return ans
        
        

0 0
原创粉丝点击