int lengthOfLastWord(string& s) {
// Write your code here
if(s.empty())
return 0;
int i=s.length()-1;
int res=0;
while(i>=0&&s[i]==' ')
{
i--;
}
while(i>=0&&s[i]!=' ')
{
res++;
i--;
}
return res;
}
思路:末尾开始判断,遇到不是空格的开始累加,知道碰到下一个空格!