C Solution.


  • 0
    N
    int lengthOfLastWord(char* s) {
        int i,c=0,lc=0;
        for(i=0;i<strlen(s);i++){
            if(*(s+i)==' '){
                if(c!=0){
                    lc=c;
                }
                c=0;
            }else{
                c++;
            }
        }
        return c==0?lc:c;
    }
    

  • 0
    L

    you call strlen() for so much times...


Log in to reply
 

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