58.Length of Last Word

class Solution {
public:
    int lengthOfLastWord(string s) {
        int right = s.size() - 1, res = 0;
        while (right >= 0 && s[right] == ' ') --right;
        while (right >= 0 && s[right] != ' ' ) {
            --right; 
            ++res;
        }
        return res;
    }
};
posted @ 2019-04-09 10:29  JohnRed  阅读(75)  评论(0)    收藏  举报