Observation
This problem gives us two numbers, num1
, num2
.
Suppose num1 < num2
, although might be arbitrary.
Therefore, it’s actually calculate 0 ~ num2
- 0 ~ num1
in every number.
The Problem : Calculate 1 ~ num
To calculate various numbers in 1 ~ num
, I use 3 steps. Let b
be the maximum that satisfy num / pow(10,b) > 0
.
- calculate
1 ~ pow(10,b)-1
, or1 ~ 999...99
, we can recursively solve this by usecount_numbers(pow(10,b)-1)
. - calculate the midst of the number, or
10...00 ~ X0...00 - 1
, where X is the leading number ofnum
. - calculate the back, or
X0...00 ~ num
Sample Code
1 |
|