坑题:最后一个单词的长度

 

 出题人语死早,并没有叙述清楚

值得注意的是,“abc       ”这个字符串是有单词存在的 所以从尾部判断的时候还要再看一下前边

class Solution {
public:
    int lengthOfLastWord(string s) {
        int len = s.size();
        int rear = len-1;
        int cnt = 0;
        for(int i = rear;i>=0;i--)
        {
            if(s[i] !=' ')
                cnt++;
            else if(cnt!=0)
             return cnt;
        }
        return cnt;
    }
};

这样写代码十分简洁

posted @ 2021-01-25 21:56  然终酒肆  阅读(36)  评论(0编辑  收藏  举报