您的位置:首页 > 编程语言 > Python开发

LeetCode--Length of Last Word (计算最后一个单词的长度)Python

2017-11-17 10:50 537 查看
题目:

给定一个字符串,字符串中包含空格和单词,计算最后一个单词的长度。

解题思路:

从最后一个单词开始计算长度,直到检测到空格,最后一个单词计算结束,返回当前的长度。

代码(Python):

class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
count = 0
for i in range(len(s)):
temp = i+1
if s[-temp]!=' ':
count = count+1
if s[-temp]==' ' and count!=0:
return count
return count
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: