class Solution {
public:
int lengthOfLastWord(const char *s) {
const char *p = s;
if( p == NULL)
return 0;
int len = 0;
while( (*p) != '\0')
{
len ++;
p++;
}
int last_len = 0;
int flag = 0;
for(int i=len-1; i>=0; i--)
{
if(s[i] == ' ' && flag == 0)
;
else if(s[i] == ' ' && flag == 1)
return last_len;
else
{
if( flag == 0 )
flag = 1;
last_len++;
}
}
return last_len;
}
};
LeetCode--Length Of Last Word
最新推荐文章于 2019-08-20 23:29:37 发布