-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path788.cpp
More file actions
28 lines (27 loc) · 788 Bytes
/
788.cpp
File metadata and controls
28 lines (27 loc) · 788 Bytes
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
class Solution {
public:
int rotatedDigits(int n) {
int res = 0;
vector<int> dp(n + 1, 0);
for (int i = 0; i <= n; ++i) {
if (i < 10) {
if (i == 0 || i == 1 || i == 8) dp[i] = 1;
else if (i == 2 || i == 5 || i == 6 || i == 9) {
dp[i] = 2;
res += 1;
}
}
else {
int prefix = i / 10;
int suffix = i % 10;
if (dp[prefix] == 0 || dp[suffix] == 0) dp[i] = 0;
else if (dp[prefix] == 1 && dp[suffix] == 1) dp[i] = 1;
else {
dp[i] = 2;
res += 1;
}
}
}
return res;
}
};