class Solution {
public:
int lengthOfLastWord(string s) {
if ("" == s)
return 0;
while (*s.rbegin() == ' ')
s.erase(--(s.rbegin().base()));
auto const pos = s.find_last_of(' ');
if (pos == string::npos)
{
//not find space
return s.length();
}
else
{
string strTemp = s.substr(pos + 1);
return strTemp.length();
}
}
};
最后一个单词的长度
最新推荐文章于 2024-03-18 10:28:50 发布