Skip to content

Commit e06f560

Browse files
authored
Update 1067-digit-count-in-range.js
1 parent 20b791b commit e06f560

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

1067-digit-count-in-range.js

+47
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
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+
148
/**
249
* @param {number} d
350
* @param {number} low

0 commit comments

Comments
 (0)