58.Length of Last Word

 

 

class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        if len(s) == 0:
            return 0
        s = s[::-1]
        length = 0
        for i,v in enumerate(s):
            if v == " ":
                if length > 0:
                    return length
            else:
                length += 1
        return length

 

posted @ 2020-05-09 15:16  星海寻梦233  阅读(86)  评论(0编辑  收藏  举报