58. Length of Last Word

trim()让这个问题变得简单多了。。

    public int lengthOfLastWord(String s) {
        s = s.trim();
        int res = 0;
        for (int i = s.length() - 1; i >= 0; i --) {
            if (s.charAt(i) == ' ') return res;
            res ++;
        }
        return res;
    }
posted @ 2016-11-06 06:57  哇呀呀..生气啦~  阅读(85)  评论(0)    收藏  举报