您的位置:首页 > 其它

【LeetCode】Length of Last Word 解题报告

2017-08-24 15:46 411 查看

【LeetCode】Length of Last Word 解题报告

标签(空格分隔): LeetCode

题目地址:https://leetcode.com/problems/length-of-last-word/description/

题目描述:

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.

Examle:

input:

"Hello World"
"Hello World "
"Hello W orld"
"Hello Wo   rld"


output:

5
5
4
3


Ways

使用库函数,方法比较简单,一行代码。

class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
return len(s.strip().split(' ')[-1])


Date

2017 年 8 月 24 日
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: