求出最后一个单词的长度,数据有全空格,空格结尾等情况,
public class Solution {
public int lengthOfLastWord(String s) {
int ans = 0;
int pre = -1, next = 0;
int i;
for( i = 0; i < s.length(); ++i){
if(s.charAt(i) == ' '){
int t = i - pre;
if(t >= 2){
ans = t - 1;
}
pre = i;
}
}
if(i - 1 >= 0 && s.charAt(i - 1) != ' '){
ans = i - pre - 1;
}
return ans;
}
}