my easy java solution


  • 0
    L
    public int lengthOfLastWord(String s) {
         if(s == null) return 0;
        int wordLen = 0;
        for(int i = s.length() - 1; i>= 0; i--){
            if(s.charAt(i) != ' '){
                wordLen++;
            }else if(wordLen > 0){
                break;
            }
        }
        return wordLen;
    }

Log in to reply
 

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