My Java solution


  • 0
    Y
    public int lengthOfLastWord(String s) {
            int len = 0;
            int i = s.length()-1;
            while (i >= 0) {
                if (s.charAt(i) == ' ') {
                    if (len > 0) { 
                        return len;
                    }
                } else {
                    len++;
                }
                
                i--;
            }
            
            return len;   // corner case: "a"
        }
    

  • 0
    D

    What is your run time?


Log in to reply
 

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