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 "))

  

posted @ 2018-10-17 12:03  September·  阅读(476)  评论(0编辑  收藏  举报