Having fun on Leetcode with C++, Java, Python & GO
Problem 58. Length of Last Word
My attempt(s)
Code
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class Solution(object):
def lengthOfLastWord(self, s):
cnt=0
leng=len(s)
if s==" ":
return 0
else:
while leng>0:
if (s[leng-1]==" " and cnt==0):
leng-=1
else:
if s[leng-1]==" ":
break
else:
cnt+=1
leng-=1
return cnt
"""
:type s: str
:rtype: int
"""
|
Explanation of idea
Good solution ref.
Code
Analysis