Length of Last Word

来源:互联网 发布:阿里云主营业务 编辑:程序博客网 时间:2024/05/17 17:54
    public int lengthOfLastWord(String s) {        // Start typing your Java solution below        // DO NOT write main() function        if(s == null | s.length() == 0) return 0;        int end = s.length() - 1;        int count = 0;        while(end >= 0 && s.charAt(end) == ' ') {            end--;        }        while(end >= 0 && s.charAt(end) != ' ') {            end--;            count++;        }        return count;    }

原创粉丝点击