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();
}
}
本文将介绍如何使用Java编程语言实现一个方法,该方法接收一个包含字母和空格的字符串作为输入,并返回该字符串中最后一个单词的长度。通过字符串分割和数组操作,可以轻松地解决这一问题。
686

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



