最主要的就是理解切割字符串的函数。
class Solution {
public:
int lengthOfLastWord(string s) {
size_t idx = s.find(' ');
while(idx != string::npos)
{
static string str;
if(idx == 0)
{
s = s.substr(1, s.size());
}
else
{
str = s.substr(0, idx);
s = s.substr(idx, s.size());
}
if(s.size() == 0)
{
return str.size();
}
idx = s.find(' ');
}
return s.size();
}
};