share my accept java solution


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

Log in to reply
 

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