Skip to content

Commit 20b791b

Browse files
authored
Update 233-number-of-digit-one.js
1 parent 83622e7 commit 20b791b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

233-number-of-digit-one.js

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
const countDigitOne = function(n) {
6+
let res = 0
7+
const str = `${n}`
8+
const len = str.length, { pow } = Math
9+
10+
for(let i = 1; i <= len; i++) {
11+
const pre = ~~(n / pow(10, i))
12+
const remain = n % (pow(10, i - 1))
13+
const post = pow(10, i - 1)
14+
res += pre * post
15+
const e = +(str[len - i])
16+
if(e > 1) {
17+
res += pow(10, i - 1)
18+
} else if(e === 1) {
19+
res += remain + 1
20+
}
21+
}
22+
23+
return res
24+
};
25+
26+
27+
// another
28+
129
/**
230
* @param {number} n
331
* @return {number}

0 commit comments

Comments
 (0)