4ms C++ solution


  • 0
    int lengthOfLastWord(string s) {
        int n=s.length()-1;
        int len=0;
        while(s[n]==' '){
            n--;
        }
        for(int i=n;i>=0;i--){
            if(('a'<=s[i]&&s[i]<='z')||('A'<=s[i]&&s[i]<='Z'))
                len++;
            else break;
        }
        return len;
    }

Log in to reply
 

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