您的位置:首页 > 其它

[Leetcode] Length of Last Word

2012-12-04 03:56 441 查看
class Solution {
public:
int lengthOfLastWord(const char *s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int length = strlen(s);
if (length == 0)
return 0;

int i = length - 1;

while (s[i] == ' ')
--i;

if (i < 0)
return 0;

int count = 0;
while (s[i] != ' ' && i >= 0)
{
++count;
--i;
}

return count;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: