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