Python simple solution without using join and split


  • 0
    A

    Wondering is this O(1) space or not? (or I have to build a reverse function?)

    class Solution:
        # @param s, a list of 1 length strings, e.g., s = ['h','e','l','l','o']
        # @return nothing
        def reverseWords(self, s):
            
            st = 0
            for i in range(len(s)):
                if s[i]==" ":
                    s[st:i]=s[st:i][::-1]
                    st = i+1
                    
            s[st:]=s[st:][::-1]
            
            s[:] = s[::-1]
            
    
    

Log in to reply
 

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