public class Solution {
public int lengthOfLastWord(String s) {
int res = 0;
if(s.length() == 0){
return res;
}
char[] sArray = s.toCharArray();
boolean startWithSpace = false;
if(sArray[sArray.length - 1] == ' '){
startWithSpace = true;
}
for(int i = sArray.length - 1; i >= 0 ; i--){
if(!startWithSpace){
if(sArray[i] != ' '){
res++;
}else{
break;
}
}else{
if(sArray[i] == ' '){
continue;
}else{
res++;
startWithSpace = false;
}
}
}
return res;
}
}
Length of Last Word
最新推荐文章于 2021-05-08 15:12:01 发布