-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1067.cpp
More file actions
27 lines (24 loc) · 725 Bytes
/
1067.cpp
File metadata and controls
27 lines (24 loc) · 725 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
class Solution {
public:
int recursive(int d, int n) {
if (n <= 9) {
return (d == 0) ? 0 : (d <= n);
}
int res = 0;
// e.g., 797
// the last place -> 79_
res += ((d == 0) ? (n / 10 - 1) : (n / 10));
// to add 797
// ^
if (n % 10 >= d) res++;
res += recursive(d, n / 10) * 10;
// to count 79x
string temp = to_string(n / 10);
// to delete 799 and 798 cases
res -= count(temp.begin(), temp.end(), d + '0') * (9 - n % 10);
return res;
}
int digitsCount(int d, int low, int high) {
return recursive(d, high) - recursive(d, low - 1);
}
};