Length of Last Word

简单题

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

 

posted @ 2015-06-02 08:00  世界到处都是小星星  阅读(116)  评论(0编辑  收藏  举报