C++ 0ms 4 lines using array instead of map


  • 0
    bool isStrobogrammatic(string num) {
            // -1 means it's not a strobogrammatic number
            int buddy[10] = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};            
            
            for (int l = 0, r = num.length() - 1; l <= r; l++, r--)
                if (buddy[num[l] - '0'] != num[r] - '0') { return false; }
            
            return true;
    }
    

Log in to reply
 

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