Skip to content

Commit 6965e20

Browse files
authored
Create 1012-numbers-with-repeated-digits.js
1 parent 6fbb9a2 commit 6965e20

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var numDupDigitsAtMostN = function(n) {
6+
const digits = [], {floor} = Math
7+
let tmp = n + 1
8+
while(tmp) {
9+
digits.push(tmp % 10)
10+
tmp = floor(tmp / 10)
11+
}
12+
let res = 0
13+
const len = digits.length
14+
let cur = 9
15+
for(let i = 0; i < len - 1; i++) {
16+
res += cur
17+
cur *= (9 - i)
18+
}
19+
cur = floor(cur / 9)
20+
const seen = Array(10).fill(false)
21+
for(let i = 0; i < len; i++) {
22+
const d = digits[len - i - 1]
23+
for(let j = (i === 0 ? 1 : 0); j < d; j++) {
24+
if(!seen[j]) res += cur
25+
}
26+
cur = floor(cur / (9 - i))
27+
if(seen[d]) break
28+
seen[d] = true
29+
}
30+
31+
return n - res
32+
};

0 commit comments

Comments
 (0)