From cfff33ddbdd4c492ae20122c5cadf13b6b46fb7b Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 10 Apr 2025 06:23:28 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.2999 No.2999.Count the Number of Powerful Integers --- .../README.md | 69 +++++++++++++++++++ .../README_EN.md | 69 +++++++++++++++++++ .../Solution.rs | 57 +++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 solution/2900-2999/2999.Count the Number of Powerful Integers/Solution.rs diff --git a/solution/2900-2999/2999.Count the Number of Powerful Integers/README.md b/solution/2900-2999/2999.Count the Number of Powerful Integers/README.md index cec5a5d000ce6..f4936db52f480 100644 --- a/solution/2900-2999/2999.Count the Number of Powerful Integers/README.md +++ b/solution/2900-2999/2999.Count the Number of Powerful Integers/README.md @@ -310,6 +310,75 @@ function numberOfPowerfulInt(start: number, finish: number, limit: number, s: st } ``` +#### Rust + +```rust +impl Solution { + pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 { + fn count(x: i64, limit: i32, s: &str) -> i64 { + let t = x.to_string(); + if t.len() < s.len() { + return 0; + } + + let t_bytes: Vec = t.bytes().collect(); + let mut f = [-1_i64; 20]; + + fn dfs( + pos: usize, + lim: bool, + t: &[u8], + s: &str, + limit: i32, + f: &mut [i64; 20], + ) -> i64 { + if t.len() < s.len() { + return 0; + } + + if !lim && f[pos] != -1 { + return f[pos]; + } + + if t.len() - pos == s.len() { + if lim { + let suffix = &t[pos..]; + let suffix_str = String::from_utf8_lossy(suffix); + return if suffix_str.as_ref() >= s { 1 } else { 0 }; + } else { + return 1; + } + } + + let mut ans = 0; + let up = if lim { + (t[pos] - b'0').min(limit as u8) + } else { + limit as u8 + }; + + for i in 0..=up { + let next_lim = lim && i == t[pos] - b'0'; + ans += dfs(pos + 1, next_lim, t, s, limit, f); + } + + if !lim { + f[pos] = ans; + } + + ans + } + + dfs(0, true, &t_bytes, s, limit, &mut f) + } + + let a = count(start - 1, limit, &s); + let b = count(finish, limit, &s); + b - a + } +} +``` + #### C# ```cs diff --git a/solution/2900-2999/2999.Count the Number of Powerful Integers/README_EN.md b/solution/2900-2999/2999.Count the Number of Powerful Integers/README_EN.md index 5ffa6ef4c116e..f2cf88994f79b 100644 --- a/solution/2900-2999/2999.Count the Number of Powerful Integers/README_EN.md +++ b/solution/2900-2999/2999.Count the Number of Powerful Integers/README_EN.md @@ -308,6 +308,75 @@ function numberOfPowerfulInt(start: number, finish: number, limit: number, s: st } ``` +#### Rust + +```rust +impl Solution { + pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 { + fn count(x: i64, limit: i32, s: &str) -> i64 { + let t = x.to_string(); + if t.len() < s.len() { + return 0; + } + + let t_bytes: Vec = t.bytes().collect(); + let mut f = [-1_i64; 20]; + + fn dfs( + pos: usize, + lim: bool, + t: &[u8], + s: &str, + limit: i32, + f: &mut [i64; 20], + ) -> i64 { + if t.len() < s.len() { + return 0; + } + + if !lim && f[pos] != -1 { + return f[pos]; + } + + if t.len() - pos == s.len() { + if lim { + let suffix = &t[pos..]; + let suffix_str = String::from_utf8_lossy(suffix); + return if suffix_str.as_ref() >= s { 1 } else { 0 }; + } else { + return 1; + } + } + + let mut ans = 0; + let up = if lim { + (t[pos] - b'0').min(limit as u8) + } else { + limit as u8 + }; + + for i in 0..=up { + let next_lim = lim && i == t[pos] - b'0'; + ans += dfs(pos + 1, next_lim, t, s, limit, f); + } + + if !lim { + f[pos] = ans; + } + + ans + } + + dfs(0, true, &t_bytes, s, limit, &mut f) + } + + let a = count(start - 1, limit, &s); + let b = count(finish, limit, &s); + b - a + } +} +``` + #### C# ```cs diff --git a/solution/2900-2999/2999.Count the Number of Powerful Integers/Solution.rs b/solution/2900-2999/2999.Count the Number of Powerful Integers/Solution.rs new file mode 100644 index 0000000000000..2ce88e7dd89ca --- /dev/null +++ b/solution/2900-2999/2999.Count the Number of Powerful Integers/Solution.rs @@ -0,0 +1,57 @@ +impl Solution { + pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 { + fn count(x: i64, limit: i32, s: &str) -> i64 { + let t = x.to_string(); + if t.len() < s.len() { + return 0; + } + + let t_bytes: Vec = t.bytes().collect(); + let mut f = [-1_i64; 20]; + + fn dfs(pos: usize, lim: bool, t: &[u8], s: &str, limit: i32, f: &mut [i64; 20]) -> i64 { + if t.len() < s.len() { + return 0; + } + + if !lim && f[pos] != -1 { + return f[pos]; + } + + if t.len() - pos == s.len() { + if lim { + let suffix = &t[pos..]; + let suffix_str = String::from_utf8_lossy(suffix); + return if suffix_str.as_ref() >= s { 1 } else { 0 }; + } else { + return 1; + } + } + + let mut ans = 0; + let up = if lim { + (t[pos] - b'0').min(limit as u8) + } else { + limit as u8 + }; + + for i in 0..=up { + let next_lim = lim && i == t[pos] - b'0'; + ans += dfs(pos + 1, next_lim, t, s, limit, f); + } + + if !lim { + f[pos] = ans; + } + + ans + } + + dfs(0, true, &t_bytes, s, limit, &mut f) + } + + let a = count(start - 1, limit, &s); + let b = count(finish, limit, &s); + b - a + } +}