NeetCode #10LC-1056EasyArrays & HashingNC Algo100
← Back to All Problems

#10 · #1056 · Confusing Number(易混淆数)

📌 Problem Statement & Constraints

A confusing number is one that becomes a different valid number when rotated 180 degrees. The valid digits are 0, 1, 6, 8, 9, which become 0, 1, 9, 8, 6 respectively. Given an integer n, return true if it is confusing. Constraints: 0 <= n <= 10^9.

💡 Core Algorithmic Approaches

  1. Rotating 180 degrees reverses the digit order and maps each digit individually.
  2. Build the rotated number by iterating the decimal digits from least significant to most significant, appending the mapped digit.
  3. If any digit is not in the rotation table, the number is not rotatable at all, so return false immediately.
  4. Compare the reconstructed number with the original. It is confusing exactly when they differ.

💻 Benchmark Python3 Implementation

class Solution:
    def confusingNumber(self, n: int) -> bool:
        rot = {"0": "0", "1": "1", "6": "9", "8": "8", "9": "6"}
        out = []
        for ch in reversed(str(n)):        # reversal handles the 180-degree flip
            if ch not in rot:
                return False               # digit cannot be rotated
            out.append(rot[ch])
        return int("".join(out)) != n      # confusing iff it changed

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(log n): one pass over the decimal digits.
💾 Space Complexity
O(log n) for the digit list, which is at most 10 characters here.

⚠️ Interview Pitfalls & Follow-ups

  • Forgetting the reversal: rotating 180 degrees flips the order, so 6 becomes 9 but 61 becomes 19, not 16.
  • Treating 0 as automatically confusing: 0 rotates to 0, which equals the original, so it is not confusing. The same holds for 8, 11, 69 vs 96... check the final comparison rather than special-casing.
  • Leaving out 2, 3, 4, 5, 7: these digits have no valid rotation, so their presence makes the answer false.
  • Using int arithmetic with % 10 and / 10: it works, but the string version is far less error-prone here because the reversal is explicit.