Java 4ms solution, easy understand, machine state


  • 0
    R
    public int lengthOfLastWord(String s) {
        int result=0;
        boolean isMetSpace = false;
        for(Character ch : s.toCharArray()){
            if(ch==' '){
                isMetSpace=true;
            }
            else{
                if(isMetSpace){
                    result=0;
                    isMetSpace=false;
                }
                result++;
            }
        }
        return result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.