python regular expression 1 line


  • 0
    D
    class Solution(object):
        def reverseWords(self, s):
            """
            :type s: str
            :rtype: str
            """
            import re
            return " ".join(re.findall("\s*(\S+)\s*", s)[::-1])
    

  • 0
    C

    another one line version
    return ' '.join( filter(None, s.split(' '))[::-1] ) if s else ''ORreturn ' '.join(s.split()[::-1])


Log in to reply
 

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