Reverse Words in a String
Problem: Reverse Words in a String
Just split the sentence into words, reverse them and combine back.
Code in Python:
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
words = s.split(" ")
res = ""
for word in words[::-1]:
if word: res += word + " "
return res[:len(res)-1]