、
class Solution:
"""
@param: s: A string
@return: the length of last word
"""
def lengthOfLastWord(self, s):
# write your code here
a = s.strip() # 去掉前后空格
count = 0
for i in range(-1, -len(a) - 1, -1):
if a[i] != " ":
count += 1
else:
break
return count
字符串也可以遍历,不用将字符串用list转换。