smilevchy's blog

Life & Study & Chasing

Leetcode_Length of Last Word

原题链接: https://oj.leetcode.com/problems/length-of-last-word/

public int lengthOfLastWord(String s) {
    if (s == null) return 0;

    int lengthOfLastWord = 0;
    boolean meetSpace = false;
    char c;

    for (int i = 0, length = s.length(); i < length; i++) {
        c = s.charAt(i);

        switch (c) {
            case ' ':
                meetSpace = true;

                break;

            default:
                if (meetSpace) {
                    lengthOfLastWord = 0;
                    lengthOfLastWord++;
                    meetSpace = false;
                } else {
                    lengthOfLastWord++;
                }

                break;
        }
    }

    return lengthOfLastWord;
}

algorithm

« Leetcode_Binary Tree Postorder Traversal Leetcode_Single Number »