python 5 lines solution


  • 0
    D
    import re
    class Solution(object):
        def reverseWords(self, s):
            """
            :type s: str
            :rtype: str
            """
            s = s.strip() # strip trailing and leading space
            s = re.sub(' +',' ',s)# strip multiple space
            s = s.split(" ")
            s =  " ".join(s[:: -1])
            return s
    
    

  • 0
    D

    return " ".join(s.split()[:: -1]) is also ok


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.