【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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章