[leetcode]Length of Last Word

class Solution {
public:
    int lengthOfLastWord(const char *s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int N = 0;
        const char *p = s;
        while(*p){
            p++;
            N++;
        }
        
        int i = N-1;
        while(i >= 0 && s[i] == ' '){
            i--;
        }
        if(i == -1) return 0;
        int end = i;
        
        while(i >= 0 && s[i] != ' '){
            i--;
        }
        int start = i;
        
        return end-start;
        
    }
};

你可能感兴趣的:([leetcode]Length of Last Word)