您的位置:首页 > 其它

Leetcode 58 Length of Last Word

2017-06-24 14:54 417 查看

Leetcode 58 Length of Last Word

#include <string>
using namespace std;

class Solution {
public:
int lengthOfLastWord(string s) {
int begin = s.length() - 1;
int count = 0;
while (s[begin] == ' ')//尾部空格要去掉,找一个循环的开始点
begin--;
for (int i = begin; i >= 0; i--){
if (s[i] != ' ')
count++;
else
break;
}
return count;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode