Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d668513

Browse files
feat: add solutions to lc problem: No.10034 (doocs#2192)
1 parent a4b8b4e commit d668513

File tree

7 files changed

+551
-6
lines changed

7 files changed

+551
-6
lines changed

‎solution/10000-10099/10034.Count the Number of Powerful Integers/README.md‎

Lines changed: 185 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,209 @@
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```python
68-
68+
class Solution:
69+
def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int:
70+
@cache
71+
def dfs(pos: int, lim: int):
72+
if len(t) < n:
73+
return 0
74+
if len(t) - pos == n:
75+
return int(s <= t[pos:]) if lim else 1
76+
up = min(int(t[pos]) if lim else 9, limit)
77+
ans = 0
78+
for i in range(up + 1):
79+
ans += dfs(pos + 1, lim and i == int(t[pos]))
80+
return ans
81+
82+
n = len(s)
83+
t = str(start - 1)
84+
a = dfs(0, True)
85+
dfs.cache_clear()
86+
t = str(finish)
87+
b = dfs(0, True)
88+
return b - a
6989
```
7090

7191
### **Java**
7292

7393
<!-- 这里可写当前语言的特殊实现逻辑 -->
7494

7595
```java
76-
96+
class Solution {
97+
private String s;
98+
private String t;
99+
private Long[] f;
100+
private int limit;
101+
102+
public long numberOfPowerfulInt(long start, long finish, int limit, String s) {
103+
this.s = s;
104+
this.limit = limit;
105+
t = String.valueOf(start - 1);
106+
f = new Long[20];
107+
long a = dfs(0, true);
108+
t = String.valueOf(finish);
109+
f = new Long[20];
110+
long b = dfs(0, true);
111+
return b - a;
112+
}
113+
114+
private long dfs(int pos, boolean lim) {
115+
if (t.length() < s.length()) {
116+
return 0;
117+
}
118+
if (!lim && f[pos] != null) {
119+
return f[pos];
120+
}
121+
if (t.length() - pos == s.length()) {
122+
return lim ? (s.compareTo(t.substring(pos)) <= 0 ? 1 : 0) : 1;
123+
}
124+
int up = lim ? t.charAt(pos) - '0' : 9;
125+
up = Math.min(up, limit);
126+
long ans = 0;
127+
for (int i = 0; i <= up; ++i) {
128+
ans += dfs(pos + 1, lim && i == (t.charAt(pos) - '0'));
129+
}
130+
if (!lim) {
131+
f[pos] = ans;
132+
}
133+
return ans;
134+
}
135+
}
77136
```
78137

79138
### **C++**
80139

81140
```cpp
82-
141+
class Solution {
142+
public:
143+
long long numberOfPowerfulInt(long long start, long long finish, int limit, string s) {
144+
string t = to_string(start - 1);
145+
long long f[20];
146+
memset(f, -1, sizeof(f));
147+
148+
function<long long(int, bool)> dfs = [&](int pos, bool lim) -> long long {
149+
if (t.size() < s.size()) {
150+
return 0;
151+
}
152+
if (!lim && f[pos] != -1) {
153+
return f[pos];
154+
}
155+
if (t.size() - pos == s.size()) {
156+
return lim ? s <= t.substr(pos) : 1;
157+
}
158+
long long ans = 0;
159+
int up = min(lim ? t[pos] - '0' : 9, limit);
160+
for (int i = 0; i <= up; ++i) {
161+
ans += dfs(pos + 1, lim && i == (t[pos] - '0'));
162+
}
163+
if (!lim) {
164+
f[pos] = ans;
165+
}
166+
return ans;
167+
};
168+
169+
long long a = dfs(0, true);
170+
t = to_string(finish);
171+
memset(f, -1, sizeof(f));
172+
long long b = dfs(0, true);
173+
return b - a;
174+
}
175+
};
83176
```
84177

85178
### **Go**
86179

87180
```go
181+
func numberOfPowerfulInt(start, finish int64, limit int, s string) int64 {
182+
t := strconv.FormatInt(start-1, 10)
183+
f := make([]int64, 20)
184+
for i := range f {
185+
f[i] = -1
186+
}
187+
188+
var dfs func(int, bool) int64
189+
dfs = func(pos int, lim bool) int64 {
190+
if len(t) < len(s) {
191+
return 0
192+
}
193+
if !lim && f[pos] != -1 {
194+
return f[pos]
195+
}
196+
if len(t)-pos == len(s) {
197+
if lim {
198+
if s <= t[pos:] {
199+
return 1
200+
}
201+
return 0
202+
}
203+
return 1
204+
}
205+
206+
ans := int64(0)
207+
up := 9
208+
if lim {
209+
up = int(t[pos] - '0')
210+
}
211+
up = min(up, limit)
212+
for i := 0; i <= up; i++ {
213+
ans += dfs(pos+1, lim && i == int(t[pos]-'0'))
214+
}
215+
if !lim {
216+
f[pos] = ans
217+
}
218+
return ans
219+
}
220+
221+
a := dfs(0, true)
222+
t = strconv.FormatInt(finish, 10)
223+
for i := range f {
224+
f[i] = -1
225+
}
226+
b := dfs(0, true)
227+
return b - a
228+
}
229+
```
88230

231+
### **TypeScript**
232+
233+
```ts
234+
function numberOfPowerfulInt(start: number, finish: number, limit: number, s: string): number {
235+
let t: string = (start - 1).toString();
236+
let f: number[] = Array(20).fill(-1);
237+
238+
const dfs = (pos: number, lim: boolean): number => {
239+
if (t.length < s.length) {
240+
return 0;
241+
}
242+
if (!lim && f[pos] !== -1) {
243+
return f[pos];
244+
}
245+
if (t.length - pos === s.length) {
246+
if (lim) {
247+
return s <= t.substring(pos) ? 1 : 0;
248+
}
249+
return 1;
250+
}
251+
252+
let ans: number = 0;
253+
const up: number = Math.min(lim ? +t[pos] : 9, limit);
254+
for (let i = 0; i <= up; i++) {
255+
ans += dfs(pos + 1, lim && i === +t[pos]);
256+
}
257+
258+
if (!lim) {
259+
f[pos] = ans;
260+
}
261+
return ans;
262+
};
263+
264+
const a: number = dfs(0, true);
265+
t = finish.toString();
266+
f = Array(20).fill(-1);
267+
const b: number = dfs(0, true);
268+
269+
return b - a;
270+
}
89271
```
90272

91273
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /