Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
思路:
- 拆分字符串,使用split函数,用
' '拆分。 - 去掉空字符,字符串最后一位为空字符。
 - reverse整个数组并连接。
 
实现:1
2
3
4
5
6
7
8def reverseWords(s):
        arr = s.split(' ')
        while '' in arr:
            arr.remove('')
        arr.reverse()
        str = ' '.join(arr)
        return str