
Input: n =6Output: trueExplanation: We get 9 after rotating 6,9is a valid number, and 9!=6.
Example 2:
1
2
3
4

Input: n =89Output: trueExplanation: We get 68 after rotating 89,68is a valid number and 68!=89.
Example 3:
1
2
3
4

Input: n =11Output: falseExplanation: We get 11 after rotating 11,11is a valid number but the value remains the same, thus 11is not a confusing number
A confusing number is one that, when rotated 180 degrees, becomes a different valid number. We can check this by mapping each digit, constructing the rotated number in reverse, and comparing it to the original.
classSolution {
publicbooleanconfusingNumber(int n) {
int orig = n, rot = 0;
int[] mp =newint[]{0,1,-1,-1,-1,-1,9,-1,8,6};
while (n > 0) {
int d = n % 10;
if (mp[d]==-1) returnfalse;
rot = rot * 10 + mp[d];
n /= 10;
}
return rot != orig;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funconfusingNumber(n: Int): Boolean {
val mp = mapOf(0 to 0, 1 to 1, 6 to 9, 8 to 8, 9 to 6)
var orig = n
var rot = 0var x = n
while (x > 0) {
val d = x % 10val v = mp[d] ?:returnfalse rot = rot * 10 + v
x /=10 }
return rot != orig
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defconfusingNumber(self, n: int) -> bool:
mp = {0:0, 1:1, 6:9, 8:8, 9:6}
orig, rot = n, 0 x = n
while x >0:
d = x %10if d notin mp:
returnFalse rot = rot *10+ mp[d]
x //=10return rot != orig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnconfusing_number(n: i32) -> bool {
let mp = [(0,0),(1,1),(6,9),(8,8),(9,6)];
letmut map = std::collections::HashMap::new();
for (k,v) in mp.iter() { map.insert(*k, *v); }
letmut orig = n;
letmut rot =0;
letmut x = n;
while x >0 {
let d = x %10;
if!map.contains_key(&d) { returnfalse; }
rot = rot *10+ map[&d];
x /=10;
}
rot != orig
}
}