leetcode 最后一个单词的长度 python

class Solution:
    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
        l = []
        for i in s[::-1].lstrip():
            if i != ' ':
                l.append(i)
            else:
                break

        return len(l)


w = Solution()
print(w.lengthOfLastWord("hello world"))
print(w.lengthOfLastWord("a "))

  

原文地址:https://www.cnblogs.com/liang3044/p/9803313.html