58. Length of Last Word

问题描述

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
If the last word does not exist, return 0.

思路

去掉空的情况
把str劈开,求最后一个元素的长度

    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
        if not s:
            return 0
        elif s.strip()=='':
            return 0
        else:
            return len(s.split()[-1])

你可能感兴趣的:(58. Length of Last Word)