美文网首页
58. Length of Last Word

58. Length of Last Word

作者: JERORO_ | 来源:发表于2018-08-06 23:59 被阅读0次

问题描述

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

      本文链接:https://www.haomeiwen.com/subject/mjbnvftx.html