Two Implementations of python


  • 0
    S
    def lengthOfLastWord(self, s):
            """
            :type s: str
            :rtype: int
            """
            count = 0
            for letter in reversed(s):
                if letter == " ":
                    if count != 0:
                        break
                    continue
                count += 1
            return count
    
    
    
    def lengthOfLastWord(self, s):
                """
                :type s: str
                :rtype: int
                """
                word = s.split()
                if len(word) == 0:
                   return 0
                return len(word[-1])

Log in to reply
 

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