4.14
无话可说
public class Solution {
/**
* @param s A string
* @return the length of last word
*/
public int lengthOfLastWord(String s) {
if(s == null){
return 0;
}
String[] tmp = s.split(" ");
int x = tmp.length;
return tmp[x-1].length();// Write your code here
}
}
本文介绍了一个简单的Java方法,该方法用于计算并返回给定字符串中最后一个单词的长度。此方法首先检查输入字符串是否为null,然后使用split方法按空格分割字符串,并返回最后一个元素的长度。
461

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



