Length of Last Word 2021-06-10 19:51

Problem Description

public int lengthOfLastWord(String s) {
    s = s.trim();
    int length = s.length();
    for (int i = length - 1; i >= 0; i--) {
        if (s.charAt(i) == ' ') {
            return length - 1 - i;
        } else if (i == 0) {
            return length;
        }
    }
    return 0;
}
Runtime Memory
0 ms 36.4 MB

henryxi leetcode list

EOF