lintcode-422

class Solution {
public:
    /**
     * @param s A string
     * @return the length of last word
     */
    int lengthOfLastWord(string& s) {
        // Write your code here
        if(0==s.size())
            return 0;

        int i,size=s.size();
        i=size-1;
        while(i>=0&&s[i]==' '){
            --i; 
        }
        if(i<0)
            return 0;
        int count=0;    
        while(i>=0&&s[i]!=' '){
            ++count;
            --i;
        }
        return count;
    }
};

你可能感兴趣的:(lintcode-422)