Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
Example:
Input: "Hello World"
Output: 5
解題思路:
這題沒啥可講的仇参,直接用Python的內(nèi)置函數(shù) .split() 分解以空格為分隔符放的字符串箩溃,存到列表中了赌,然后返回最后一個單詞的長度。
Python實現(xiàn):
class Solution:
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
sl = s.split()
if len(sl) == 0:
return 0
return len(sl[-1])
a = "Hello World "
b = Solution()
print(b.lengthOfLastWord(a)) # 5