File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } d
3
+ * @param {number } low
4
+ * @param {number } high
5
+ * @return {number }
6
+ */
7
+ const digitsCount = function ( d , low , high ) {
8
+ return countDigit ( high , d ) - countDigit ( low - 1 , d )
9
+ } ;
10
+ function countDigit ( limit , d ) {
11
+ let res = 0
12
+ const str = `${ limit } `
13
+ const len = str . length
14
+ const { pow } = Math
15
+ if ( d === 0 ) {
16
+ for ( let i = 1 ; i < len ; i ++ ) {
17
+ const pre = ~ ~ ( limit / pow ( 10 , i ) )
18
+ const post = pow ( 10 , i - 1 )
19
+ res += ( pre - 1 ) * post
20
+ const e = + str [ len - i ]
21
+ if ( e > d ) {
22
+ res += post
23
+ } else if ( e === d ) {
24
+ res += ( limit % post ) + 1
25
+ }
26
+ }
27
+ } else {
28
+ for ( let i = 1 ; i <= len ; i ++ ) {
29
+ const pre = ~ ~ ( limit / pow ( 10 , i ) )
30
+ const post = pow ( 10 , i - 1 )
31
+ res += pre * post
32
+ const e = + str [ len - i ]
33
+ if ( e > d ) {
34
+ res += post
35
+ } else if ( e === d ) {
36
+ res += ( limit % post ) + 1
37
+ }
38
+ }
39
+ }
40
+
41
+
42
+ return res
43
+ }
44
+
45
+
46
+ // another
47
+
1
48
/**
2
49
* @param {number } d
3
50
* @param {number } low
You can’t perform that action at this time.
0 commit comments