python script. beat 94%


  • 0
    M

    python script. beat 94%

    class Solution(object):
        def lengthOfLastWord(self, s):
            """
            :type s: str
            :rtype: int
            """
            if len(s) == 0:
                return 0
    
            s = s[::-1]
            
            i = 0
            l = 0
            begin = False
            while i<len(s):
                if s[i]!=' ':
                    l +=1
                    begin = True
                elif s[i] == ' ' and begin == True:
                    return l
                i+=1
            return l
    

Log in to reply
 

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