File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
function adjust(num, limit, s) {
3+
let sn = parseInt(s);
4+
let sufMod = 10 ** s.length;
5+
let suf = num % sufMod;
6+
num = Math.floor(num / sufMod);
7+
8+
if (suf < sn) --num;
9+
10+
if (num <= 0) return num + 1;
11+
12+
let sNum = num.toString();
13+
let res = sNum.charCodeAt(0) - 48;
14+
let tight = 1;
15+
16+
if (res > limit) {
17+
return (limit + 1) ** sNum.length;
18+
}
19+
20+
for (let i = 1; i < sNum.length; ++i) {
21+
res *= (limit + 1);
22+
23+
if (tight) {
24+
let c = sNum.charCodeAt(i) - 48;
25+
if (c > limit) {
26+
tight = 0;
27+
res += limit + 1;
28+
} else {
29+
res += c;
30+
}
31+
}
32+
}
33+
34+
return res + tight;
35+
}
36+
37+
var numberOfPowerfulInt = function (start, finish, limit, s) {
38+
--start;
39+
let ss = adjust(start, limit, s);
40+
let ff = adjust(finish, limit, s);
41+
return ff - ss;
42+
};

0 commit comments

Comments
 (0)