⚡Flashcard Drill➔
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.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Arrays & Hashing Problems

中文速览与面试要点

易混淆数

给定一个整数 n,判断它是否是一个「易混淆数」。将数字整体旋转 180 度后,有效数字的映射为:0 → 0,1 → 1,6 → 9,8 → 8,9 → 6。若 n 的十进制表示中 含有 上述以外的数字(2、3、4、5、7),则 n 不是易混淆数。若旋转后得到的数字与原数字 不同 ,则 n 是易混淆数。约束:0 ≤ n ≤ 10^9。

核心要点

  • 逐步模拟旋转(标准解) :把 n 转成字符串, 从右往左 逐位处理(旋转 180° 等价于「逆序 + 逐位映射」)。
  • 无效数字的判定 :任一数位不在 0, 1, 6, 8, 9 中,直接返回 False。
  • 与 246 的区别(重要) :246 题(中心对称数)要求旋转后 等于 原数;本题要求旋转后 不等于 原数,正好相反。
  • 前导零的处理 :映射结果可能以 0 开头(如 n = 100 旋转得到 001),转成整数后前导零自然消失,因此直接比较整数值即可。

常见陷阱

  • 把「不同」写成「相同」 :本题要求旋转后 不等于 原数,与 246 中心对称数正好相反。这是最高频的审题错误。
  • 忘记处理前导零导致误判 :若用字符串直接比较( "".join(out) != s ),n = 100 时映射结果是 "001",与 "100" 不等,返回 True——恰好正确;但 n = 1 时映射结果是 "1",与 "1" 相等返回 False,也正确。真正会出错的是 不转 int 又忘记逆序 的写法。
  • 忘记逆序 :旋转 180° 是「逆序 + 映射」两个动作,只做映射不逆序会得到完全错误的结果(如 6 → 6 而非 9)。
  • 把 2、3、4、5、7 当成映射到自身 :这些数字旋转后不构成有效数字,必须返回 False,不能忽略。

参考实现

class Solution:
    def confusingNumber(self, n: int) -> bool:
        rotate = {0: 0, 1: 1, 6: 9, 8: 8, 9: 6}
        s = str(n)
        out = []
        for ch in reversed(s):          # 逆序 = 旋转后的阅读顺序
            d = int(ch)
            if d not in rotate:
                return False            # 含无效数字
            out.append(str(rotate[d]))
        return int(''.join(out)) != n   # 旋转后必须与原数不同