1ms C solution,use end to start


  • 0
    L

    int lengthOfLastWord(char* s) {

    if(*s == '\0') return 0;
    
    char *tmp=s;
    while(*tmp++ != '\0'){
    
        if(*tmp != ' ') break;
    }
    
    if(*--tmp == '\0') return 0;
    
    
    tmp=s;
    while(*tmp++ != '\0');
    
    
    tmp--;tmp--;
    
    while(*tmp == ' ') tmp--;
    int i=0;
    while(*tmp != ' ' && tmp >= s ) {
        i++;
        tmp--;
    }
    
    return i;
    

    }


Log in to reply
 

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