您的位置:首页 > 理论基础 > 计算机网络

https://leetcode.com/problems/length-of-last-word/

2015-05-13 22:28 190 查看
https://leetcode.com/problems/length-of-last-word/

简单 用string.split(' ')

有可能有连续空格 因此从后往前找第一个非空值计算长度

class Solution:
# @param {string} s
# @return {integer}
def lengthOfLastWord(self, s):
list=[]
if s.isspace()==True:
return 0
elif s=='':
return 0
else:
list=s.split(' ')
for i in range(len(list))[::-1]:
if list[i]!='':
return len(list[i])

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: