leetcode_1067
Given an integer d between 0 and 9, and two positive integers low and high as lower and upper bounds, respectively. Return the number of times that d occurs as a digit in all integers between low and high, including the bounds low and high.
Note:
0 <= d <= 9
1 <= low <= high <= 2×10^8
Solutions
math
This problem is a generalization of
problem 233
.The method is the same as in
problem 233
except for the case whend
equals to0
. Since the highest number can not be0
, we need to remove these impossible cases.For example:
When counting the number of
0
in the second lowest digit,n = 123
.Following the rule in
problem 233
, the number of 0 in this position is composed of two parts:The main part:
(223 / 100) * 10 = 20
ie:01, 02, 03, ... 09 101, 102, 103 ... 109
. It's clear that the first10
numbers are invalid.The remainder part:
min(223 % 100 - 10 * 0 + 1, 10) = 10
, ie:201 202 203 204 205 ... 209
.Thus the deduced count offsets the count of invalid numbers in the main part.
Last updated
Was this helpful?