0
点赞
收藏
分享

微信扫一扫

58. Length of Last Word(最后一个单词的长度)


题目地址: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.

Example:


Input: "Hello World" Output: 5


 

给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。

如果不存在最后一个单词,请返回 0 。

说明:一个单词是指由字母组成,但不包含任何空格的字符串。

示例:


输入: "Hello World" 输出: 5


 

class Solution {
    public int lengthOfLastWord(String s) {
        String[] str = s.split(" +");
        int len = str.length;
        if (len > 0)    return str[len - 1].length();
        return 0;
    }
}

58. Length of Last Word(最后一个单词的长度)_大小写

 

========================Talk is cheap, show me the code=========================

举报

相关推荐

0 条评论