題目:
解題思路:
代碼:
public class LC92 {
public int lengthOfLastWord(String s) {
int end = s.length() - 1;
while(end >= 0 && s.charAt(end) == ' ') end--;
if(end < 0) return 0;
int start = end;
while(start >= 0 && s.charAt(start) != ' ') start--;
return end - start;
}
public static void main(String[] args) {
LC92 lc = new LC92();
System.out.println(lc.lengthOfLastWord("Hello World"));
}
}