Length of Last Word

来源:互联网 发布:马伊琍 哪里漂亮 知乎 编辑:程序博客网 时间:2024/05/19 09:49
class Solution {public:    int lengthOfLastWord(string s)     {        if(s.empty())            return 0;        int count=0;        bool firstspace=true;        const int n=s.size();        for(int i=n-1;i>=0;--i)        {            if(s[i]!=' ')            {                 ++count;                 firstspace=false;            }               if(s[i]==' '&& firstspace)                continue;            if(s[i]==' '&& !firstspace)                return count;                    }        return count;                    }};

0 0
原创粉丝点击