LeetCode 1056: Confusing Number Solution

Master LeetCode problem 1056 (Confusing Number), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1056. Confusing Number

Easy

Problem Explanation

Explanation:

Given a number, we need to determine if it is a confusing number. A confusing number is a number where each digit is replaced by another digit when the number is rotated 180 degrees.

To determine if a number is confusing, we can create a mapping of digits that will become other digits when rotated 180 degrees. Then we iterate through the number digit by digit, applying the mapping to each digit and checking if the resulting number is different from the original number. If any digit is not a valid confusing digit, we return false. :

Solution Code

class Solution {
    public boolean confusingNumber(int N) {
        int original = N;
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, 0);
        map.put(1, 1);
        map.put(6, 9);
        map.put(8, 8);
        map.put(9, 6);
        
        int rotated = 0;
        while (N > 0) {
            int digit = N % 10;
            if (!map.containsKey(digit)) {
                return false;
            }
            rotated = rotated * 10 + map.get(digit);
            N /= 10;
        }
        
        return rotated != original;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1056 (Confusing Number)?

This page provides optimized solutions for LeetCode problem 1056 (Confusing Number) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1056 (Confusing Number)?

The time complexity for LeetCode 1056 (Confusing Number) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1056 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1056 in Java, C++, or Python.

Back to LeetCode Solutions