`
hcx2013
  • 浏览: 88895 次
社区版块
存档分类
最新评论

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 characters only.

For example, 
Given s = "Hello World",
return 5.

public class Solution {
    public int lengthOfLastWord(String s) {
        if (s==null || "".equals(s.trim())) {
        	return 0;
        }
        String[] arr = s.split(" ");
        return arr[arr.length-1].length();
    }
}

 

分享到:
评论

相关推荐

    last-word-length.zip_Last Word

    Console.WriteLine($"The length of the last word is: {lastWordLength}"); } } ``` 这段代码首先移除了输入字符串两端的空格,然后使用`Split(' ')`方法将字符串拆分为单词数组。如果数组为空(即输入字符串只...

    js-leetcode题解之58-length-of-last-word.js

    js js_leetcode题解之58-length-of-last-word.js

    c++-c++编程基础之leetcode题解第58题最后一个单词的长度.zip

    在本压缩包中,我们关注的是C++编程基础与LeetCode题目的结合,特别是针对第58题“最后一个单词的长度”(Length of Last Word)的解决方案。LeetCode是一个在线平台,提供各种算法题目,旨在提升程序员的编程技能和...

    C语言-leetcode题解之58-length-of-last-word.c

    c语言入门 C语言_leetcode题解之58-length-of-last-word.c

    算法刷题笔记leetcode/lintcode

    - Length of Last Word(最后一个单词的长度) - Count and Say(猜数字序列) - **Integer Array**(整型数组操作) - Remove Element(移除元素) - Zero Sum Subarray(连续子数组的最大和) - Subarray Sum...

    LeetCode最全代码

    318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...

    leetcode浇花-LCSolutions:我的力扣解决方案

    Length of Last Word #0066 - Plus One #0083 - Remove Duplicates from Sorted List #0118 - Pascal's Triangle #0121 - Best Time to Buy and Sell Stock #0125 - Valid Palindrome #0136 - Single Number #0167 -...

    Leetcode代码以及解答(2)

    Length of Last Word **知识点:** - **问题描述:** - 给定一个字符串,返回最后一个单词的长度。 - **解决方案分析:** - **双指针:** - 从字符串末尾开始遍历,找到第一个非空格字符 `end`。 - 从 `end`...

    leetcode中国-leetcode:leetcode刷题

    leetcode中国 我自己的leetcode刷题记录 ###[20150920] Valid Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome ...Length of Last Word,字符串处理.细节题 Rever

    JS字符串和正则表达式[收集].pdf

    var last_three_letters = the_word.substring(the_word.length - 3, the_word.length); // "key" ``` `substring`与`charAt`不同,它能获取多个连续的字符。 除了这些基础方法,JavaScript还提供了其他字符串操作...

    kgb档案压缩console版+源码

    leading characters of y are known, the range of possible x narrows, so the leading digits can be output as they become known. For decompression, as the digits of x are read, the set of possible y ...

    JS字符串和正则表达式.pdf

    var the_last_letter = the_word.charAt(the_word.length - 1); // "y" ``` 注意,字符串的索引是从0开始的,因此最后一个字符的索引是`length - 1`。 `substring()`方法用于提取字符串的一部分,它接受两个参数,...

    TCL TK 语言8.5编程指导

    Determining the length of a string 49 Comparing strings 50 Comparing a string of characters 51 Locating the first instance of a character 52 Locating the index of a character 53 Determining the class ...

    leetcode题库-LeetCode-Rust:听说刷题建repo是立flag一般的行为?

    leetcode题库 LeetCode-Rust 听说 LeetCode 添加了 Rust 支持, 这岂不是双倍的快(fu)乐(za)? 于是来体验一下 用 Rust 写题真是酸爽无比啊! (各种意义上的) 刷了几天的感受 ...length_of_last_word -- --nocapture

    freemarker总结

    last 返回sequence最后一个值 sequence?reverse 反转sequence的值 sequence?size 返回sequence的大小 sequence?sort 对sequence按里面的对象toString()的结果进行排序 sequence?sort_by(value) 对sequence 按...

    spssw-184.zip

    outSPSS.addDateVar("update", DataConstants.DATE_TYPE_17, "Date of last update", null); // Create value labels ValueLabels valueLabels = new ValueLabels(); valueLabels.putLabel("Asia", "Asia ...

    58. 最后一个单词的长度

    最后一个单词的长度](https://leetcode-cn.com/problems/length-of-last-word/)题目描述solution idea一次遍历参考文献 leetcode58:58. 最后一个单词的长度 题目描述 给定一个仅包含大小写字母和空格 ’ ’ 的字符...

    表层进firstly to begin with

    时间相关的过渡词如 "after a while", "afterward", "again", "also", "at last", "at length", "at that time", "before", "besides", "earlier", "eventually", "finally", "formerly", "further", "furthermore",...

Global site tag (gtag.js) - Google Analytics