C solution in one go, O(n), 0ms, without library function call


  • 0
    L

    "Three Pointer" approach

    int lengthOfLastWord(char* s) {
        char *t = s, *r = s;
        while (*r == ' ') r++;
        while (*r) {
            s = t = r;
            while (*s != ' ' && *s != '\0') s++;
            r = s;
            while (*r == ' ') r++;
        }
        return s - t;
    }
    

Log in to reply
 

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