class Solution {
public:
int lengthOfLastWord(const char *s) {
int len=strlen(s);
int start=0;
int end=0;
bool wordStart=false;
bool wordEnd=false;
for(int i=len-1;i>=0;--i)
{
if((wordEnd)&&(s[i]==' '))
{
wordStart=true;
start=i+1;
break;
}
if(s[i]==' ')
{
continue;
}
if(!wordEnd)
{
end=i+1;
wordEnd=true;
}
}
return end-start;
}
};
Length of Last Word
最新推荐文章于 2024-09-14 18:52:56 发布