It looks easy but need to consider the case that there are white spaces on the end of string.
class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
for i in reversed(range(len(s))):
if s[i] == ' ' and result > 0:
break
if s[i] != ' ':
result += 1
return result
No comments:
Post a Comment