-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountDigitOne.js
More file actions
42 lines (40 loc) · 788 Bytes
/
countDigitOne.js
File metadata and controls
42 lines (40 loc) · 788 Bytes
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
/**
* @param {number} n
* @return {number}
*/
var countDigitOne = function(n) {
// 1 in every tens place
// 10^k for every magnitude of 10^k
// find tens place less than n = int(n / 10) * 10
// maybe use memoization?
let [q, x, ans] = [n, 1, 0]
while(q > 0) {
let digit = q % 10
q = q/10 << 0
ans += q * x
let g = 0
if(digit === 1) {
g = n % x + 1
ans += n % x + 1
} else if(digit > 0) {
ans += x
g = x
}
console.log(q * x, g)
// console.log(digit, q, ans, x)
x *= 10
}
return ans
}
let q = 4211
countDigitOne(q)
// 422 + 1
// 420 + 2
// 400 + 100
// 1000
let z = []
for(let i = 0; i <= q; ++i) {
if((i/10 << 0) % 10 === 1)
z.push(i)
}
console.log(z.length)