Skip to content

Commit 7d586db

Browse files
authored
Update 233-number-of-digit-one.js
1 parent 3a2a0ba commit 7d586db

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

233-number-of-digit-one.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
const countDigitOne = function(n) {
6+
let res = 0
7+
const s = `${n}`
8+
const len = s.length, {floor, pow} = Math
9+
10+
for(let i = 1; i <= len; i++) {
11+
const np = pow(10, i - 1)
12+
const pre = floor(n / pow(10, i))
13+
const remain = n % np
14+
15+
res += pre * np
16+
const e = +s[len - i]
17+
if(e > 1) {
18+
res += np
19+
} else if(e === 1) {
20+
res += remain + 1
21+
}
22+
}
23+
24+
return res
25+
};
26+
27+
28+
// another
29+
30+
131
/**
232
* @param {number} n
333
* @return {number}

0 commit comments

Comments
 (0)