EasyNo. 1056LeetCode Premium

Confusing Number — LeetCode 1056 Python Solution

Topics
Math
Reading time
2 min

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

PythonSolution
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 != n

Complexity

Time
O(n) or O(1)
Space · auxiliary
O(1)

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

Related problems
No.ProblemDifficulty
0002Add Two NumbersMedium
0007Reverse IntegerMedium
0009Palindrome NumberEasy
0043Multiply StringsMedium
0048Rotate ImageMedium
0050Pow(x, n)Medium

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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.