Skip to content

Commit 10b23d6

Browse files
committed
feat(233): ✨数位 DP 解决 233
1 parent fbcc06d commit 10b23d6

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

Rank/182/solution4.js renamed to Rank/182/5371/solution1.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,24 @@ const findGoodStrings = (n, s1, s2, evil) => {
66
return 0;
77
}
88

9+
const record = new Set();
10+
for (let i = 0; i < evil.length; i++) {
11+
record.add(evil[i]);
12+
}
13+
914
let c1 = 0
1015
for (let i = s1[0].charCodeAt(0) + 1; i < s2[0].charCodeAt(0); i++) {
1116
if (evil.indexOf(String.fromCharCode(i)) === -1) {
1217
c1++;
1318
}
1419
}
20+
1521
ans += getCount(c1, evil.length, n - 1);
1622

1723
for (let i = 1; i < s1.length; i++) {
24+
if (record.has(s1[i - 1])) {
25+
break;
26+
}
1827
const item = s1[i];
1928
let count = 0;
2029
let start = item.charCodeAt(0);
@@ -26,7 +35,13 @@ const findGoodStrings = (n, s1, s2, evil) => {
2635
start++;
2736
}
2837
ans += getCount(count, evil.length, n - i - 1);
38+
39+
}
2940

41+
for (let i = 0; i < s2.length; i++) {
42+
if (record.has(s2[i - 1])) {
43+
break;
44+
}
3045
if(s2[i - 1] !== s1[i - 1]) {
3146
const item2 = s2[i];
3247
let count2 = 0;
@@ -40,8 +55,8 @@ const findGoodStrings = (n, s1, s2, evil) => {
4055

4156
ans += getCount(count2, evil.length, n - i - 1);
4257
}
43-
4458
}
59+
4560
return ans;
4661
}
4762

dynamic-programming/233/solution1.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* https://leetcode-cn.com/problems/number-of-digit-one/
3+
*
4+
* 233. 数字 1 的个数
5+
*
6+
* Hard
7+
*/
8+
const countDigitOne = n => {
9+
if (n <= 0) {
10+
return 0;
11+
}
12+
13+
if (n === 1) {
14+
return 1;
15+
}
16+
17+
const list = String(n).split('');
18+
let ans = 0;
19+
for (let i = 0, max = list.length; i < max; i++) {
20+
const currentItem = list[i];
21+
22+
if (currentItem > 1) {
23+
let leftCount = Number.parseInt(list.slice(0, i).join('')) + 1;
24+
if (Number.isNaN(leftCount)) {
25+
leftCount = 1;
26+
}
27+
ans += leftCount * Math.pow(10, max - i - 1);
28+
} else if (currentItem === '1') {
29+
let leftCount = Number.parseInt(list.slice(0, i).join(''));
30+
if (Number.isNaN(leftCount)) {
31+
leftCount = 0;
32+
}
33+
ans += leftCount * Math.pow(10, max - i - 1);
34+
let rightCount = Number.parseInt(list.slice(i + 1).join(''));
35+
if (Number.isNaN(rightCount)) {
36+
rightCount = 0;
37+
}
38+
ans += rightCount + 1;
39+
} else {
40+
let leftCount = Number.parseInt(list.slice(0, i).join(''));
41+
if (Number.isNaN(leftCount)) {
42+
leftCount = 1;
43+
}
44+
ans += leftCount * Math.pow(10, max - i - 1);
45+
}
46+
47+
}
48+
49+
return ans;
50+
}

0 commit comments

Comments
 (0)