leetcode(43)------58.Length of Last Word

版权声明:本文为博主原创文章,未经允许不得转载。 https://blog.csdn.net/wem603947175/article/details/82226591

58. 最后一个单词的长度

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.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

输入: "Hello World"
输出: 5

Python代码实现:

class Solution:
    def lengthOfLastWord(self, s):
        sa = s.split()
        if sa != []:
            return len(sa[-1])
        else:
            return 0

猜你喜欢

转载自blog.csdn.net/wem603947175/article/details/82226591