C solution, 1ms, once traversal, cache the last non-zero world.


  • 0
    4
    int lengthOfLastWord(char *s) {
        int len = 0;
        int last = 0;
        
        for (; *s; s++)
        {
            if (*s==' ')
            {
                if (len > 0)/* to fliter end of space*/
                {
                    last = len;
                }
                len = 0;
            }
            else
            {
                len++;
            }
        }
        
        return len>0 ? len:last;
    }

Log in to reply
 

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