Tips:
int index = s.length() - 1;
int count = 0; //count the word length
//Find the begin letter of the last word
while (index >= 0 && s.charAt(index) == ' ')
index--;
if (index == -1) //all blanks
return 0;
//Start to count the length of the last word
while (index >= 0 && s.charAt(index) != ' ') {
count++;
index--;
}
return count;
If和后面的while可以合并,因为后面的while不执行的话,count还是0