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.
我利用java的库函数,split做的。
public int lengthOfLastWord(String s) {
if (s == null || s.length() == 0)
return

该博客讨论了如何解决LeetCode上的一个问题——找出字符串中最后一个单词的长度。作者提到,可以通过Java的库函数split实现,但如果不能使用库函数,则可以采用字符数组从后向前遍历的方法来检测非空字符,从而找到最后一个单词的长度。以'Hello World'为例,返回结果为5。
订阅专栏 解锁全文
133

被折叠的 条评论
为什么被折叠?



