【LeetCode OJ】Reverse Words in a String

Posted on 2014-03-13 02:52  卢泽尔  阅读(286)  评论(2)    收藏  举报

Problem link:

http://oj.leetcode.com/problems/reverse-words-in-a-string/

 

Given an input string, reverse the string word by word. For example,
Given s = "the sky is blue",
return "blue is sky the".

LeetCode OJ supports Python now!

The solution for this problem is extremely easy... What you need is only three build-in methods: string.split(), string.join(), and list.reverse().

class Solution:
    # @param s, a string
    # @return a string
    def reverseWords(self, s):
        res = s.split()
        res.reverse()
        return " ".join( res )

 That is it...