58. Length of Last Word

Leetcode Diary

Posted by Xudong on September 10, 2020

Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word (last word means the last appearing word if we loop from left to right) in the string.

If the last word does not exist, return 0.

Note: A word is defined as a maximal substring consisting of non-space characters only.

Example

Input: "Hello World"
Output: 5

Thoughts

  • 需要注意最后有空格的情况,要对字符串做一个类似trim()的操作

Code(JAVA)

public int lengthOfLastWord(String s) {
    int res = 0;
    boolean noCharacter = true;
    for(int i = s.length()-1; i >=0; i--) {
        if(s.charAt(i) == ' ') {
            if(noCharacter)
                continue;
            else
                break;
        }
        res ++;
        noCharacter = false;
    }
    return res;
}