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
- Rotating 180 degrees reverses the digit order and maps each digit individually.
- Build the rotated number by iterating the decimal digits from least significant to most significant, appending the mapped digit.
- If any digit is not in the rotation table, the number is not rotatable at all, so return
falseimmediately. - 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
6becomes9but61becomes19, not16. - Treating 0 as automatically confusing:
0rotates to0, which equals the original, so it is not confusing. The same holds for8,11,69vs96... 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
intarithmetic with% 10and/ 10: it works, but the string version is far less error-prone here because the reversal is explicit.