My take 3 ms C++


  • 0
    D
    int lengthOfLastWord(string s) 
    {
        int result(0);
        for (int k = s.length() - 1; k >= 0; --k )
            if (::isalpha(s[k]) == 0)
                if (result > 0)
                    break;
            else
                ++result;
        return result;
    }
    

    Using ::isalpha(), could be slower than just checking for space :)


Log in to reply
 

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