58. Length of Last Word

原题链接:https://leetcode.com/problems/length-of-last-word/description/
这题目没啥难度,稍微懂点编程均可独立完成:

class Solution {
    public int lengthOfLastWord(String s) {
        int len = 0;
        
        char[] chars = s.toCharArray();
        for (int i = chars.length - 1; i >= 0; i--) {
            if (chars[i] == ' ') {
                if (len > 0) {
                    return len;
                }
            } else {
                len++;
            }
        }
        
        return len;
    }
}
posted @ 2018-03-15 17:05  optor  阅读(171)  评论(1编辑  收藏  举报