One line python


  • -1
    L
    class Solution:
            # @param s, a string
            # @return a string
            def reverseWords(self, s):
                   return " ".join([i for i in s.split(" ") if i!=""][::-1])
    

    first split the string into list ,and then picked out the words,finally ,do the reverse


  • 0
    S

    Pay attention to "Writing code? Select all code then click on the {} button to preserve code formatting.” above text editor.


  • 0
    S

    nice , pythonic!


  • 0
    L
    class Solution:
        # @param s, a string
        # @return a string
        def reverseWords(self, s):
            return ' '.join(list(reversed(s.split())))

Log in to reply
 

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