[LeetCode] 58. Length of Last Word java

来源:互联网 发布:创维网络电视价格 编辑:程序博客网 时间:2024/06/03 11:30
    /**58. Length of Last Word     * @param s     * @returnint     */    public int lengthOfLastWord(String s) {        if (s == null || s.length() == 0) {            return 0;        }        int end = s.length() - 1;        while(end >= 0 && s.charAt(end) == ' ') {            end--;        }        int start = end;        while(start >= 0 && s.charAt(start) != ' ') {            start--;        }        return end-start;    }    //顺序!!!先判断index是否有效,在判断值
0 0
原创粉丝点击