解题思路:
使用库函数,先将首尾空白字符串用strip()去掉,再用split()按照空格分开,形成一个列表,返回最后一个元素的长度。
def lengthOfLastWord(s):
if s.strip() == '':
return 0
lists = s.strip().split(' ')
return len(lists[-1])
解题思路:
使用库函数,先将首尾空白字符串用strip()去掉,再用split()按照空格分开,形成一个列表,返回最后一个元素的长度。
def lengthOfLastWord(s):
if s.strip() == '':
return 0
lists = s.strip().split(' ')
return len(lists[-1])