-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlengthOfLastWord.py
More file actions
50 lines (38 loc) · 1.32 KB
/
lengthOfLastWord.py
File metadata and controls
50 lines (38 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- encoding: utf-8 -*-
'''
@project : LeetCode
@File : lengthOfLastWord
@Contact : 9824373@qq.com
@Desc :
给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度。
如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词。
如果不存在最后一个单词,请返回 0 。
说明:一个单词是指仅由字母组成、不包含任何空格的 最大子字符串。
示例:
输入: "Hello World"
输出: 5
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/length-of-last-word
@Modify Time @Author @Version @Desciption
------------ ------- -------- -----------
2020-02-20 zhan 1.0 None
'''
class Solution:
def lengthOfLastWord(self, s: str) -> int:
# "a "
s = s.strip()
ans = None
for char in s:
if char == " ":
ans = None
elif ans is None:
ans = char
else:
ans +=char
ans = 0 if ans == None else len(ans)
return ans
if __name__ == '__main__':
s = "ww ad "
ans = Solution().lengthOfLastWord(s)
print(ans)