Skip to content

Commit 150c31b

Browse files
authored
Create 233-number-of-digit-one.js
1 parent 4b35d9e commit 150c31b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

233-number-of-digit-one.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
const countDigitOne = function(n) {
6+
let count = 0
7+
for (let m = 1; m <= n; m *= 10) {
8+
const a = Math.floor(n / m)
9+
const b = n % m
10+
if (a % 10 > 1) {
11+
count += (Math.floor(a / 10) + 1) * m
12+
} else if (a % 10 === 1) {
13+
count += Math.floor(a / 10) * m + b + 1
14+
} else {
15+
count += Math.floor(a / 10) * m
16+
}
17+
}
18+
return count
19+
}

0 commit comments

Comments
 (0)