-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution2.cpp
31 lines (31 loc) · 936 Bytes
/
Solution2.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
class Solution {
public:
int rotatedDigits(int n) {
string s = to_string(n);
int m = s.size();
int f[m][2];
memset(f, -1, sizeof(f));
auto dfs = [&](this auto&& dfs, int i, int ok, bool limit) -> int {
if (i >= m) {
return ok;
}
if (!limit && f[i][ok] != -1) {
return f[i][ok];
}
int up = limit ? s[i] - '0' : 9;
int ans = 0;
for (int j = 0; j <= up; ++j) {
if (j == 0 || j == 1 || j == 8) {
ans += dfs(i + 1, ok, limit && j == up);
} else if (j == 2 || j == 5 || j == 6 || j == 9) {
ans += dfs(i + 1, 1, limit && j == up);
}
}
if (!limit) {
f[i][ok] = ans;
}
return ans;
};
return dfs(0, 0, true);
}
};