class Solution {
public:
int lengthOfLastWord(string s) {
auto p = (int)(s.size()-1);
string res;
while(p>=0 && s[p] == ' '){
p--;
}
while(p>=0 && s[p]!=' '){
res.push_back(s[p]);
p--;
}
reverse(res.begin(), res.end());
return res.size();
}
};
转载于:https://www.cnblogs.com/theodoric008/p/9376675.html
本文提供了一个使用C++编写的简单程序,该程序能够计算并返回输入字符串中最后一个单词的长度。通过迭代字符串的字符并利用标准库函数,此方法有效地解决了问题。

522

被折叠的 条评论
为什么被折叠?



