LeetCode-Length of Last Word

很直接 注意corner 

""

" "

"a   "

public class Solution {
    public int lengthOfLastWord(String s) {
        if ( s == null || s.length() == 0)
            return 0;
        int length = s.length();
        int pos = length - 1;
        while ( pos >= 0 && s.charAt(pos) == ' '){
            pos --;
        }
        int lastlen = 0;
        while ( pos >= 0 && s.charAt(pos) != ' '){
            lastlen ++;
            pos --;
        }
        return lastlen;
    }
}


你可能感兴趣的:(LeetCode-Length of Last Word)