[C++] More fun without the library, make your hands dirty!


  • 0
    S

    Single? You must be in [0, 1, 8]. (code & 0x7)

    Couple? You two must have the same non-zero code.

    bool isStrobogrammatic(string num) {
        const int match[] = {
         // 0, 1, 2, 3, 4, 5,  6, 7, 8,  9
            1, 2, 0, 0, 0, 0,  8, 0, 4, 16, // boy code
            1, 2, 0, 0, 0, 0, 16, 0, 4,  8  // girl code
        };
        int l = 0, r = (int)num.size() - 1;
        int boy, girl;
        while (l < r)
        {
            boy = num[l] - '0';
            girl = num[r] - '0' + 10;
            ++l, --r;
            if (!(match[boy] & match[girl]))
                return false;
        }
        return l == r ? (match[num[l] - '0'] & 7) : true; // works when num == ""
    }

Log in to reply
 

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