Skip to content

Commit 45a5e75

Browse files
authored
Update 233-number-of-digit-one.js
1 parent 150c31b commit 45a5e75

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
@@ -17,3 +17,22 @@ const countDigitOne = function(n) {
1717
}
1818
return count
1919
}
20+
21+
// another
22+
23+
/**
24+
* @param {number} n
25+
* @return {number}
26+
*/
27+
const countDigitOne = function(n) {
28+
if (n <= 0) return 0
29+
let ones = 0
30+
for (let i = 1, q = n; i <= n; i *= 10, q = (q / 10) >> 0) {
31+
let pre = (n / (i * 10)) >> 0,
32+
cur = q % 10,
33+
suf = n % i
34+
ones += pre * i
35+
ones += 1 < cur ? i : 1 == cur ? suf + 1 : 0
36+
}
37+
return ones
38+
}

0 commit comments

Comments
 (0)