leetcode 1088 - Confusing Number II

https://leetcode.com/problems/confusing-number-ii/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public:
int confusingNumberII(int N) {
int rotated[] = {0,1,-1,-1,-1,-1,9,-1,8,6};
int next[] = {0,1,6,6,6,6,6,8,8,9};
int total = 0;
int n = 1;

while (n <= N) {
int num1 = n;
int num2 = 0;
unsigned long tmp = 1;

bool valid = true;

while (num1) {
int cur = num1 % 10;
num1 = num1 / 10;

if (rotated[cur] == -1) {
cur = next[cur];
n = num1 * tmp * 10 + cur * tmp;
valid = false;
break;
} else {
num2 = num2 * 10 + rotated[cur];
}

tmp = tmp * 10;
}

if (valid) {
if (num2 != n)
total++;
n++;
}
}

return total;
}
};