Confusing Number

easy math digits rotation

Problem

A confusing number is a number that, when rotated 180 degrees, becomes a different valid number. Each digit must remain a valid digit after rotation: 0→0, 1→1, 6→9, 8→8, 9→6. The digits 2, 3, 4, 5, 7 become invalid when rotated. Given an integer n, return true if n is a confusing number, otherwise false.

Inputn = 6
Outputtrue
6 rotated 180° becomes 9, which differs from 6, so it is confusing.

def confusing_number(n):
    rot = {0: 0, 1: 1, 6: 9, 8: 8, 9: 6}
    original, rotated = n, 0
    while n > 0:
        d = n % 10
        if d not in rot:
            return False
        rotated = rotated * 10 + rot[d]
        n //= 10
    return rotated != original
function confusingNumber(n) {
  const rot = { 0: 0, 1: 1, 6: 9, 8: 8, 9: 6 };
  const original = n;
  let rotated = 0;
  while (n > 0) {
    const d = n % 10;
    if (!(d in rot)) return false;
    rotated = rotated * 10 + rot[d];
    n = Math.floor(n / 10);
  }
  return rotated !== original;
}
class Solution {
    public boolean confusingNumber(int n) {
        int[] rot = { 0, 1, -1, -1, -1, -1, 9, -1, 8, 6 };
        long original = n, rotated = 0;
        while (n > 0) {
            int d = n % 10;
            if (rot[d] < 0) return false;
            rotated = rotated * 10 + rot[d];
            n /= 10;
        }
        return rotated != original;
    }
}
bool confusingNumber(int n) {
    int rot[10] = { 0, 1, -1, -1, -1, -1, 9, -1, 8, 6 };
    long original = n, rotated = 0;
    while (n > 0) {
        int d = n % 10;
        if (rot[d] < 0) return false;
        rotated = rotated * 10 + rot[d];
        n /= 10;
    }
    return rotated != original;
}
Time: O(log n) Space: O(1)