Leetcode 细节实现 Length of Last Word

Length of Last Word

Total Accepted: 17518 Total
Submissions: 59526My Submissions

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.

For example,

Given s = "Hello World",

return 5.

题意:返回一个句子的最后一个单词的长度

思路:将字符串分割成单词,然后返回最后一个的长度即可

class Solution:
    # @param s, a string
    # @return an integer
    def lengthOfLastWord(self, s):
        s_split = s.split()
        return 0 if len(s_split) == 0 else len(s_split[-1])
时间: 2024-11-13 11:31:31

Leetcode 细节实现 Length of Last Word的相关文章

leetcode || 58、Length of Last Word

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

Leetcode(58)题解:Length of Last Word

https://leetcode.com/problems/length-of-last-word/ 题目: 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 de

【一天一道LeetCode】#58. Length of Last Word

一天一道LeetCode系列 (一)题目 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 cons

【LeetCode】58 - Length of Last Word

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 cha

LeetCode OJ:Length of Last Word(最后一个词的长度)

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 cha

【算法】LeetCode算法题-Length Of Last Word

这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是58).给定一个字符串,包含戴尔字母.小写字母和空格,返回最后一个单词的长度,如果最后一个单词不存在则返回0.另外,单词不包含空格.例如: 输入: "Hello World" 输出: 5 说明:最后一个单词为world,其长度为5 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 0

【Leetcode】Length of Last Word

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 cha

[LeetCode][JavaScript]Length of Last Word

Length of Last Word 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 consi

leetcode 题解: Length of Last Word

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