forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1067.cpp
32 lines (32 loc) · 773 Bytes
/
1067.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
class Solution
{
public:
int digitsCount(int d, int low, int high)
{
return digitCounts(d, high) - digitCounts(d, low-1);
}
private:
int digitCounts(int k, int n)
{
int base = 1, cnt = 0;
while (n / base >= 1)
{
int cur = n / base % 10;
int l = n % base;
int h = n / (base*10);
if (cur == k)
{
if (k == 0) cnt += (h - 1) * base + l + 1;
else cnt += 1 + base * h + l;
}
else if (cur < k) cnt += base * h;
else
{
if (k == 0) cnt += base * h;
else cnt += base * (h + 1);
}
base *= 10;
}
return cnt;
}
};