0ms C solution, easy to understand


  • 1
    M
    int lengthOfLastWord(char* s) {
        if (s[0] == '\0') return 0;
        int len = strlen(s);
        char* p = s + len -1;
        while(p != s && *p == ' ') --p;
        char* q = p;
        while(p != s && *p != ' ') --p;
        return q-p + (*p != ' ');
    }

Log in to reply
 

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