Skip to content

Commit cfff33d

Browse files
committed
feat: add rust solution to lc problem: No.2999
No.2999.Count the Number of Powerful Integers
1 parent c90ca0e commit cfff33d

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

solution/2900-2999/2999.Count the Number of Powerful Integers/README.md

+69
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,75 @@ function numberOfPowerfulInt(start: number, finish: number, limit: number, s: st
310310
}
311311
```
312312

313+
#### Rust
314+
315+
```rust
316+
impl Solution {
317+
pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 {
318+
fn count(x: i64, limit: i32, s: &str) -> i64 {
319+
let t = x.to_string();
320+
if t.len() < s.len() {
321+
return 0;
322+
}
323+
324+
let t_bytes: Vec<u8> = t.bytes().collect();
325+
let mut f = [-1_i64; 20];
326+
327+
fn dfs(
328+
pos: usize,
329+
lim: bool,
330+
t: &[u8],
331+
s: &str,
332+
limit: i32,
333+
f: &mut [i64; 20],
334+
) -> i64 {
335+
if t.len() < s.len() {
336+
return 0;
337+
}
338+
339+
if !lim && f[pos] != -1 {
340+
return f[pos];
341+
}
342+
343+
if t.len() - pos == s.len() {
344+
if lim {
345+
let suffix = &t[pos..];
346+
let suffix_str = String::from_utf8_lossy(suffix);
347+
return if suffix_str.as_ref() >= s { 1 } else { 0 };
348+
} else {
349+
return 1;
350+
}
351+
}
352+
353+
let mut ans = 0;
354+
let up = if lim {
355+
(t[pos] - b'0').min(limit as u8)
356+
} else {
357+
limit as u8
358+
};
359+
360+
for i in 0..=up {
361+
let next_lim = lim && i == t[pos] - b'0';
362+
ans += dfs(pos + 1, next_lim, t, s, limit, f);
363+
}
364+
365+
if !lim {
366+
f[pos] = ans;
367+
}
368+
369+
ans
370+
}
371+
372+
dfs(0, true, &t_bytes, s, limit, &mut f)
373+
}
374+
375+
let a = count(start - 1, limit, &s);
376+
let b = count(finish, limit, &s);
377+
b - a
378+
}
379+
}
380+
```
381+
313382
#### C#
314383

315384
```cs

solution/2900-2999/2999.Count the Number of Powerful Integers/README_EN.md

+69
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,75 @@ function numberOfPowerfulInt(start: number, finish: number, limit: number, s: st
308308
}
309309
```
310310

311+
#### Rust
312+
313+
```rust
314+
impl Solution {
315+
pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 {
316+
fn count(x: i64, limit: i32, s: &str) -> i64 {
317+
let t = x.to_string();
318+
if t.len() < s.len() {
319+
return 0;
320+
}
321+
322+
let t_bytes: Vec<u8> = t.bytes().collect();
323+
let mut f = [-1_i64; 20];
324+
325+
fn dfs(
326+
pos: usize,
327+
lim: bool,
328+
t: &[u8],
329+
s: &str,
330+
limit: i32,
331+
f: &mut [i64; 20],
332+
) -> i64 {
333+
if t.len() < s.len() {
334+
return 0;
335+
}
336+
337+
if !lim && f[pos] != -1 {
338+
return f[pos];
339+
}
340+
341+
if t.len() - pos == s.len() {
342+
if lim {
343+
let suffix = &t[pos..];
344+
let suffix_str = String::from_utf8_lossy(suffix);
345+
return if suffix_str.as_ref() >= s { 1 } else { 0 };
346+
} else {
347+
return 1;
348+
}
349+
}
350+
351+
let mut ans = 0;
352+
let up = if lim {
353+
(t[pos] - b'0').min(limit as u8)
354+
} else {
355+
limit as u8
356+
};
357+
358+
for i in 0..=up {
359+
let next_lim = lim && i == t[pos] - b'0';
360+
ans += dfs(pos + 1, next_lim, t, s, limit, f);
361+
}
362+
363+
if !lim {
364+
f[pos] = ans;
365+
}
366+
367+
ans
368+
}
369+
370+
dfs(0, true, &t_bytes, s, limit, &mut f)
371+
}
372+
373+
let a = count(start - 1, limit, &s);
374+
let b = count(finish, limit, &s);
375+
b - a
376+
}
377+
}
378+
```
379+
311380
#### C#
312381

313382
```cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
impl Solution {
2+
pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 {
3+
fn count(x: i64, limit: i32, s: &str) -> i64 {
4+
let t = x.to_string();
5+
if t.len() < s.len() {
6+
return 0;
7+
}
8+
9+
let t_bytes: Vec<u8> = t.bytes().collect();
10+
let mut f = [-1_i64; 20];
11+
12+
fn dfs(pos: usize, lim: bool, t: &[u8], s: &str, limit: i32, f: &mut [i64; 20]) -> i64 {
13+
if t.len() < s.len() {
14+
return 0;
15+
}
16+
17+
if !lim && f[pos] != -1 {
18+
return f[pos];
19+
}
20+
21+
if t.len() - pos == s.len() {
22+
if lim {
23+
let suffix = &t[pos..];
24+
let suffix_str = String::from_utf8_lossy(suffix);
25+
return if suffix_str.as_ref() >= s { 1 } else { 0 };
26+
} else {
27+
return 1;
28+
}
29+
}
30+
31+
let mut ans = 0;
32+
let up = if lim {
33+
(t[pos] - b'0').min(limit as u8)
34+
} else {
35+
limit as u8
36+
};
37+
38+
for i in 0..=up {
39+
let next_lim = lim && i == t[pos] - b'0';
40+
ans += dfs(pos + 1, next_lim, t, s, limit, f);
41+
}
42+
43+
if !lim {
44+
f[pos] = ans;
45+
}
46+
47+
ans
48+
}
49+
50+
dfs(0, true, &t_bytes, s, limit, &mut f)
51+
}
52+
53+
let a = count(start - 1, limit, &s);
54+
let b = count(finish, limit, &s);
55+
b - a
56+
}
57+
}

0 commit comments

Comments
 (0)