【LeetCode】Algorithm 51~60:58

前言

本系列博客为平时刷LeetCode的记录,每十道题一篇博客,持续更新,所有代码详见GitHub:https://github.com/roguesir/LeetCode-Algorithm

58. Length of Last Word

Introduce

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

Solution

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

https://leetcode.com/submissions/detail/202036869/

  • 更新时间:2019-02-27
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章