-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotated-digits.cpp
61 lines (51 loc) · 1.04 KB
/
rotated-digits.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright (c) 2018 Christopher Friedt
*
* SPDX-License-Identifier: MIT
*/
#include <unordered_map>
#include <unordered_set>
using namespace std;
class Solution {
public:
int rotatedDigits(int N) {
int total = 0;
for (int x = 1; x <= N; x++) {
int xx;
int digit;
int y;
int k;
for (k = 1, xx = x, digit = xx % 10, y = 0; xx;
xx /= 10, digit = xx % 10, k *= 10) {
if (3 == digit || 4 == digit || 7 == digit) {
goto just_continue;
}
if (!(0 == digit || 1 == digit || 8 == digit)) {
switch (digit) {
case 2:
digit = 5;
break;
case 5:
digit = 2;
break;
case 6:
digit = 9;
break;
case 9:
digit = 6;
break;
}
}
y += k * digit;
}
// the number must be changed
if (x == y) {
continue;
}
total++;
just_continue:
continue;
}
return total;
}
};