C++ one-liner (effectively :-)


  • 2
    M

    The two tricks are using a reverse iterator and using a string instead of an explicit map/hash.

            bool isStrobogrammatic(string num)
            { 
                auto M = (num.size() + 1) / 2;
                
                auto okay = equal(
                    num.begin(), num.begin() + M, num.rbegin(),
                    [](char lhs, char rhs)
                    { return "01xxxx9x86"[lhs - '0'] == rhs; });
    
                return okay;
            }
    

Log in to reply
 

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