A simple C solution[Accepted]


  • 0
    B
    int lengthOfLastWord(char* s) {
        int len=strlen(s);
        int count1=0;
        int count2=0;
        int i=len-1;
        for(;i>=0;i--){
            if(s[i]!=' '){
                count1=i;
                break;
            }
        }
        if(i<0){return 0;}
        for(;i>=0;i--){
            if(s[i]==' '){return count1-i;}
        }
        return count1+1;
    }
    

Log in to reply
 

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