判断字符A是不是空格字符:Character.isSpace(A)
class Solution {
public int lengthOfLastWord(String s) {
int maxLength = 0;
int i=s.length()-1;
int j=0;
while(i>=0){
if(Character.isSpace(s.charAt(i))&&j>0)return j;
if(!Character.isSpace(s.charAt(i))) j++;
i--;
}
return j;
}
}
网友评论