卖萌的弱渣

I am stupid, I am hungry.

Reverse Words in a String

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

Example,

Given s = “the sky is blue”,

return “blue is sky the”.

Solution

(Reverse-Words-in-a-String.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        ret = ""
        if not s:
            return ret

        words = s.split()
        for i in range(len(words)-1,-1,-1):
            ret = ret+words[i] + " "
        return ret[:len(ret)-1]