【LeetCode】Length of Last Word 解题报告

来源:互联网 发布:三菱触摸屏编程软件 编辑:程序博客网 时间:2024/06/06 01:27

【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:

5543

Ways

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

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

Date

2017 年 8 月 24 日