-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1067-digit-count-in-range.js
More file actions
60 lines (53 loc) · 1.54 KB
/
1067-digit-count-in-range.js
File metadata and controls
60 lines (53 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* 1067. Digit Count in Range
* https://leetcode.com/problems/digit-count-in-range/
* Difficulty: Hard
*
* Given a single-digit integer d and two integers low and high, return the number of times
* that d occurs as a digit in all integers in the inclusive range [low, high].
*/
/**
* @param {number} d
* @param {number} low
* @param {number} high
* @return {number}
*/
var digitsCount = function(d, low, high) {
return countDigits(high) - countDigits(low - 1);
function countDigits(num) {
if (num < 0) return 0;
const str = num.toString();
const n = str.length;
let count = 0;
for (let pos = 0; pos < n; pos++) {
const left = pos > 0 ? parseInt(str.substring(0, pos)) : 0;
const current = parseInt(str[pos]);
const right = pos < n - 1 ? parseInt(str.substring(pos + 1)) : 0;
const rightLength = n - pos - 1;
const rightPower = Math.pow(10, rightLength);
if (d === 0) {
if (pos === 0) {
continue;
}
if (current > d) {
count += (left - 1) * rightPower + rightPower;
} else if (current === d) {
count += (left - 1) * rightPower + right + 1;
} else {
if (left > 0) {
count += (left - 1) * rightPower + rightPower;
}
}
} else {
if (current > d) {
count += (left + 1) * rightPower;
} else if (current === d) {
count += left * rightPower + right + 1;
} else {
count += left * rightPower;
}
}
}
return count;
}
};