[leetcode]Length of Last Word

简单题。

public class Solution {

    public int lengthOfLastWord(String s) {

        // Start typing your Java solution below

        // DO NOT write main() function

        int len = s.length();

        if (len == 0) return 0;

        boolean meetWord = false;

        

        int count = 0;

        for (int i = len-1; i >= 0; i--) {

            char c = s.charAt(i);

            if (isSpace(c) && !meetWord) continue;

            if (isSpace(c) && meetWord) break;

            meetWord = true;

            count++;

        }

        return count;

        

    }

    

    private boolean isSpace(char c) {

        return c == ' ';

    }

}

  

你可能感兴趣的:(LeetCode)