Skip to content

Commit 4e61e60

Browse files
authored
update: 添加问题“2999.统计强大整数的数目”的代码和题解(#870)
* 2999: WA.py (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: WA.py - all 0(#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: AC.py - fix.(high.(0->9)+min(high, limit))(#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: WA.cpp (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: TLE.cpp | fix.(char->int) (#869) accepted https://github.com/LetMeFly666/LeetCode/pull/863\#pullrequestreview-2760402878 at last commit b8188e8 Signed-off-by: LetMeFly666 <[email protected]> * 2999: WA.cpp | fix.cache (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: WA.cpp | fix.cache (#869) Signed-off-by: LetMeFly666 <[email protected]> * vscode: auto generated settings (#869) 刚在聊天 Signed-off-by: LetMeFly666 <[email protected]> * 2999: WA.cpp | 对拍 (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: AC.cpp | cache.(int->ll) (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: CE.java (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: WA.java (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: AC.java | AC,70.30%,86.73% | fix.(i++->d++|this.limit=limit) (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: WA.go (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: AC.go - AC,100.00%,8.71% (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: 题解half版本 (#869) Signed-off-by: LetMeFly666 <[email protected]> * 2999: 题解就是为了重新解释这句话 (#869) Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“2999.统计强大整数的数目”的代码和题解(#870) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 32e9143 commit 4e61e60

7 files changed

+519
-1
lines changed

.vscode/settings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"*.pro": "plaintext",
44
"*.wxss": "css",
55
"*.wxml": "html",
6+
".commitmsg": "git-commit",
67
"array": "cpp",
78
"atomic": "cpp",
89
"*.tcc": "cpp",
@@ -84,7 +85,11 @@
8485
"memory": "cpp",
8586
"memory_resource": "cpp",
8687
"set": "cpp",
87-
"math.h": "c"
88+
"math.h": "c",
89+
"__hash_table": "cpp",
90+
"__split_buffer": "cpp",
91+
"__tree": "cpp",
92+
"queue": "cpp"
8893
},
8994
"editor.mouseWheelZoom": true,
9095
"workbench.tree.enableStickyScroll": true,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-12 22:20:48
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-12 22:37:40
6+
'''
7+
from functools import cache
8+
9+
class Solution:
10+
@cache
11+
def dfs(self, n: int, limitLow: bool, limitHigh: bool) -> int:
12+
if n == self.n:
13+
return 1
14+
low = self.start[n] if limitLow else 0
15+
high = self.finish[n] if limitHigh else 9
16+
ans = 0
17+
if n < self.free: # 什么都可以
18+
for d in range(low, min(high, self.limit) + 1):
19+
ans += self.dfs(n + 1, limitLow and d == low, limitHigh and d == high)
20+
else:
21+
x = self.s[n - self.free]
22+
if low <= x <= high:
23+
ans = self.dfs(n + 1, limitLow and x == low, limitHigh and x == high)
24+
return ans
25+
26+
def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int:
27+
self.finish = list(map(int, str(finish)))
28+
self.n = len(self.finish)
29+
self.start = list(map(int, str(start).zfill(self.n)))
30+
self.limit = limit
31+
self.free = self.n - len(s)
32+
self.s = list(map(int, s))
33+
return self.dfs(0, True, True)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-13 11:03:19
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-13 11:56:04
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
13+
class Solution {
14+
private:
15+
int limit, n, nonFixed;
16+
string suffix, start, finish;
17+
unordered_map<int, ll> cache;
18+
19+
ll dfs(int i, bool limitLow, bool limitHigh) {
20+
if (i == n) {
21+
return 1;
22+
}
23+
if (!limitLow && !limitHigh && cache.count(i)) {
24+
return cache[i];
25+
}
26+
int low = limitLow ? start[i] - '0' : 0;
27+
int high = limitHigh ? finish[i] - '0' : 9;
28+
ll ans = 0;
29+
if (i < nonFixed) {
30+
for (int d = low; d <= min(high, limit); d++) {
31+
ans += dfs(i + 1, limitLow && d == low, limitHigh && d == high);
32+
}
33+
} else {
34+
int x = suffix[i - nonFixed] - '0';
35+
if (low <= x && x <= high) { // 题目限制一定小于limit
36+
ans = dfs(i + 1, limitLow && x == low, limitHigh && x == high);
37+
}
38+
}
39+
if (!limitLow && !limitHigh) {
40+
cache[i] = ans;
41+
}
42+
return ans;
43+
}
44+
public:
45+
long long numberOfPowerfulInt(long long start, long long finish, int limit, string s) {
46+
this->limit = limit;
47+
suffix = move(s);
48+
this->finish = to_string(finish);
49+
n = this->finish.size();
50+
this->start = to_string(start);
51+
this->start = string(n - this->start.size(), '0') + this->start;
52+
nonFixed = n - this->suffix.size();
53+
54+
return dfs(0, true, true);
55+
}
56+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-13 13:38:32
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-13 14:00:33
6+
*/
7+
package main
8+
9+
import (
10+
"strconv"
11+
"strings"
12+
)
13+
14+
func dfs2999(i int, limitLow, limitHigh bool, start, finish string, nonFixed int, cache []int64, limit int, suffix string) (ans int64) {
15+
if i == len(start) {
16+
return 1
17+
}
18+
if !limitLow && !limitHigh && cache[i] != -1 {
19+
return cache[i]
20+
}
21+
22+
var low, high int
23+
if limitLow {
24+
low = int(start[i]) - int('0')
25+
} else {
26+
low = 0
27+
}
28+
if limitHigh {
29+
high = int(finish[i]) - int('0')
30+
} else {
31+
high = 9
32+
}
33+
if i < nonFixed {
34+
for d := low; d <= min(high, limit); d++ {
35+
ans += dfs2999(i + 1, limitLow && d == low, limitHigh && d == high, start, finish, nonFixed, cache, limit, suffix)
36+
}
37+
} else {
38+
d := int(suffix[i - nonFixed]) - int('0')
39+
if low <= d && d <= high {
40+
ans = dfs2999(i + 1, limitLow && d == low, limitHigh && d == high, start, finish, nonFixed, cache, limit, suffix)
41+
}
42+
}
43+
if !limitLow && !limitHigh {
44+
cache[i] = ans
45+
}
46+
return ans
47+
}
48+
49+
func numberOfPowerfulInt(start int64, finish int64, limit int, s string) int64 {
50+
finish_str := strconv.FormatInt(finish, 10)
51+
cache := make([]int64, len(finish_str) + 1)
52+
for i := range cache {
53+
cache[i] = -1
54+
}
55+
start_str := strconv.FormatInt(start, 10)
56+
start_str = strings.Repeat("0", len(finish_str) - len(start_str)) + start_str
57+
nonFixed := len(finish_str) - len(s)
58+
return dfs2999(0, true, true, start_str, finish_str, nonFixed, cache, limit, s)
59+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-13 13:00:58
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-13 13:37:04
6+
* @Description: AC,70.30%,86.73%
7+
*/
8+
import java.util.Arrays;
9+
10+
class Solution {
11+
private int n, nonFixed, limit;
12+
private String start, finish, suffix;
13+
private long[] cache;
14+
15+
private long dfs(int i, boolean limitLow, boolean limitHigh) {
16+
if (i == n) {
17+
return 1;
18+
}
19+
if (!limitLow && !limitHigh && cache[i] != -1) {
20+
return cache[i];
21+
}
22+
int low = limitLow ? start.charAt(i) - '0' : 0;
23+
int high = limitHigh ? finish.charAt(i) - '0' : 9;
24+
long ans = 0;
25+
if (i < nonFixed) {
26+
for (int d = low; d <= Math.min(limit, high); d++) {
27+
ans += dfs(i + 1, limitLow && d == low, limitHigh && d == high);
28+
}
29+
} else {
30+
int x = suffix.charAt(i - nonFixed) - '0';
31+
if (low <= x && x <= high) {
32+
ans = dfs(i + 1, limitLow && x == low, limitHigh && x == high);
33+
}
34+
}
35+
if (!limitLow && !limitHigh) {
36+
cache[i] = ans;
37+
}
38+
return ans;
39+
}
40+
41+
public long numberOfPowerfulInt(long start, long finish, int limit, String s) {
42+
this.finish = String.valueOf(finish);
43+
n = this.finish.length();
44+
this.start = String.valueOf(start);
45+
this.start = "0".repeat(n - this.start.length()) + this.start;
46+
this.limit = limit;
47+
nonFixed = n - s.length();
48+
cache = new long[n];
49+
Arrays.fill(cache, -1);
50+
suffix = s;
51+
return dfs(0, true, true);
52+
}
53+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@
867867
|2970.统计移除递增子数组的数目I|简单|<a href="https://leetcode.cn/problems/count-the-number-of-incremovable-subarrays-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/10/LeetCode%202970.%E7%BB%9F%E8%AE%A1%E7%A7%BB%E9%99%A4%E9%80%92%E5%A2%9E%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AEI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140310063" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-incremovable-subarrays-i/solutions/2836851/letmefly-2970tong-ji-yi-chu-di-zeng-zi-s-3d2z/" target="_blank">LeetCode题解</a>|
868868
|2974.最小数字游戏|简单|<a href="https://leetcode.cn/problems/minimum-number-game/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/12/LeetCode%202974.%E6%9C%80%E5%B0%8F%E6%95%B0%E5%AD%97%E6%B8%B8%E6%88%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140365205" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-game/solutions/2840437/letmefly-2974zui-xiao-shu-zi-you-xi-pai-ib5em/" target="_blank">LeetCode题解</a>|
869869
|2982.找出出现至少三次的最长特殊子字符串II|中等|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/30/LeetCode%202982.%E6%89%BE%E5%87%BA%E5%87%BA%E7%8E%B0%E8%87%B3%E5%B0%91%E4%B8%89%E6%AC%A1%E7%9A%84%E6%9C%80%E9%95%BF%E7%89%B9%E6%AE%8A%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139334864" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/solutions/2795996/letmefly-2982zhao-chu-chu-xian-zhi-shao-9ubg1/" target="_blank">LeetCode题解</a>|
870+
|2999.统计强大整数的数目|困难|<a href="https://leetcode.cn/problems/count-the-number-of-powerful-integers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/12/LeetCode%202999.%E7%BB%9F%E8%AE%A1%E5%BC%BA%E5%A4%A7%E6%95%B4%E6%95%B0%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147191672" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-powerful-integers/solutions/3649695/letmefly-2999tong-ji-qiang-da-zheng-shu-jnya8/" target="_blank">LeetCode题解</a>|
870871
|3011.判断一个数组是否可以变为有序|中等|<a href="https://leetcode.cn/problems/find-if-array-can-be-sorted/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/13/LeetCode%203011.%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%95%B0%E7%BB%84%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E5%8F%98%E4%B8%BA%E6%9C%89%E5%BA%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140391465" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-if-array-can-be-sorted/solutions/2841665/letmefly-3011pan-duan-yi-ge-shu-zu-shi-f-i9ck/" target="_blank">LeetCode题解</a>|
871872
|3019.按键变更的次数|简单|<a href="https://leetcode.cn/problems/number-of-changing-keys/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/07/LeetCode%203019.%E6%8C%89%E9%94%AE%E5%8F%98%E6%9B%B4%E7%9A%84%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144983704" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-changing-keys/solutions/3040949/letmefly-3019an-jian-bian-geng-de-ci-shu-pgzx/" target="_blank">LeetCode题解</a>|
872873
|3033.修改矩阵|简单|<a href="https://leetcode.cn/problems/modify-the-matrix/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/05/LeetCode%203033.%E4%BF%AE%E6%94%B9%E7%9F%A9%E9%98%B5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140219034" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/modify-the-matrix/solutions/2832430/letmefly-3033xiu-gai-ju-zhen-yuan-di-xiu-e0cy/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)