classSolution:deflengthOfLastWord(self, s:str)->int:
end =len(s)-1while end >=0and s[end]==' ':
end-=1
start = end
while start >=0and s[start]!=' ':
start-=1return end-start
Leetcode58-最后一个单词的长度思路:代码:class Solution: def lengthOfLastWord(self, s: str) -> int: end = len(s)-1 while end >= 0 and s[end] == ' ': end-=1 start = end while start >= 0 and s[start] != ' ':