#coding=gbk ''' 58. Length of Last Word 返回字符串中最后一个空格后面的非空字符的长度 Input: "Hello World" Output: 5 ''' class Solution: def lengthOfLastWord(self, s: str) -> int: length=len(s) if length==0: return 0 s+=' ' length+=1 output=[0]# 记录最后一个空格字符后面的单词长度 temp=0 flag = False for i in range(length): if s[i]==' ': if flag==False:# 如果上个一个扫描到的字符不是空格 output.append(temp) temp=0 flag=True else: temp+=1 flag=False # print(output) return output[-1] # print(output) # return output[-1] if output[-1]>0 else if __name__=='__main__': max_value=Solution().lengthOfLastWord("a") print(max_value)
leetcode 58 Length of Last Word
最新推荐文章于 2020-06-04 04:41:26 发布