Leetcode 58

class Solution {
public:
    int lengthOfLastWord(string s) {
        int n = s.size();
        int res = 0;
        int j = 0;
        for(j=n-1;j >= 0;j--){
            if(s[j] != ' ')
                break;
        }
        for(int i=j;i >= 0;i--){
            if(s[i] == ' ') break;
            res++;
        }
        return res;
    }
};
原文地址:https://www.cnblogs.com/cunyusup/p/9760496.html