Confusing Number — LeetCode 1056 Python Solution
- Problem
- #1056
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
A confusing number is a number that when rotated 180 degrees becomes a different number with each digit valid. We can rotate digits of a number by 180 degrees to form new digits.
Example
- Input
- n = 6
- Output
- true
- Explanation
- We get 9 after rotating 6, 9 is a valid number, and 9 != 6.
Python solution
class Solution:
def confusingNumber(self, n: int) -> bool:
x, y = n, 0
d = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6]
while x:
x, v = divmod(x, 10)
if d[v] < 0:
return False
y = y * 10 + d[v]
return y != nComplexity
| Measure | Complexity |
|---|---|
| Time | O(n) or O(1) |
| Space | O(1) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1056. Confusing Number is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1056. Confusing Number?
- LeetCode 1056. Confusing Number is rated Easy on LeetCode.
- What topics does LeetCode 1056. Confusing Number cover?
- LeetCode 1056. Confusing Number is tagged Math on LeetCode.
- Is LeetCode 1056. Confusing Number a premium problem?
- Yes. LeetCode 1056. Confusing Number is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.