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...