58. Length of Last Word

Total Accepted: 83878
Total Submissions: 291685
Difficulty: Easy

Given a string _s_ consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example,
Given _s_ = "Hello World", return 5.

Java:

1
2
3
4
5
6
7
8
9
10
public class Solution {
    public int lengthOfLastWord(String s) {
        s = s.trim();
        if (s.equals(""))
            return 0;
        int res = 0;
        for (int i = s.length() - 1; i > -1 && s.charAt(i) != ' '; ++res, --i);
        return res;
    }
}

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。