From 58c361f6787f2d4bf4ac575cb90ce813573dcdd4 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 25 Apr 2025 06:17:42 +0800 Subject: [PATCH 1/3] Update README.md --- .../README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/solution/2800-2899/2845.Count of Interesting Subarrays/README.md b/solution/2800-2899/2845.Count of Interesting Subarrays/README.md index 0935acfe76c62..812f37098d38b 100644 --- a/solution/2800-2899/2845.Count of Interesting Subarrays/README.md +++ b/solution/2800-2899/2845.Count of Interesting Subarrays/README.md @@ -208,6 +208,35 @@ function countInterestingSubarrays(nums: number[], modulo: number, k: number): n } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn count_interesting_subarrays(nums: Vec, modulo: i32, k: i32) -> i64 { + let mut arr: Vec = nums + .iter() + .map(|&x| if x % modulo == k { 1 } else { 0 }) + .collect(); + let mut cnt: HashMap = HashMap::new(); + cnt.insert(0, 1); + + let mut ans: i64 = 0; + let mut s: i32 = 0; + + for x in arr { + s += x; + let key = (s - k).rem_euclid(modulo); + ans += *cnt.get(&key).unwrap_or(&0); + *cnt.entry(s % modulo).or_insert(0) += 1; + } + + ans + } +} +``` + From 2c95b29d321e09f1b9b3d4e4770ec92403dd1348 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 25 Apr 2025 06:18:55 +0800 Subject: [PATCH 2/3] Create Solution.rs --- .../Solution.rs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 solution/2800-2899/2845.Count of Interesting Subarrays/Solution.rs diff --git a/solution/2800-2899/2845.Count of Interesting Subarrays/Solution.rs b/solution/2800-2899/2845.Count of Interesting Subarrays/Solution.rs new file mode 100644 index 0000000000000..7658bea9e79ef --- /dev/null +++ b/solution/2800-2899/2845.Count of Interesting Subarrays/Solution.rs @@ -0,0 +1,24 @@ +use std::collections::HashMap; + +impl Solution { + pub fn count_interesting_subarrays(nums: Vec, modulo: i32, k: i32) -> i64 { + let mut arr: Vec = nums + .iter() + .map(|&x| if x % modulo == k { 1 } else { 0 }) + .collect(); + let mut cnt: HashMap = HashMap::new(); + cnt.insert(0, 1); + + let mut ans: i64 = 0; + let mut s: i32 = 0; + + for x in arr { + s += x; + let key = (s - k).rem_euclid(modulo); + ans += *cnt.get(&key).unwrap_or(&0); + *cnt.entry(s % modulo).or_insert(0) += 1; + } + + ans + } +} From a73335d4b0cb23b741a70deb98b18f367edd11b8 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 25 Apr 2025 06:19:15 +0800 Subject: [PATCH 3/3] Update README_EN.md --- .../README_EN.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/solution/2800-2899/2845.Count of Interesting Subarrays/README_EN.md b/solution/2800-2899/2845.Count of Interesting Subarrays/README_EN.md index 043784d59ce20..49fb57a6e472c 100644 --- a/solution/2800-2899/2845.Count of Interesting Subarrays/README_EN.md +++ b/solution/2800-2899/2845.Count of Interesting Subarrays/README_EN.md @@ -206,6 +206,35 @@ function countInterestingSubarrays(nums: number[], modulo: number, k: number): n } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn count_interesting_subarrays(nums: Vec, modulo: i32, k: i32) -> i64 { + let mut arr: Vec = nums + .iter() + .map(|&x| if x % modulo == k { 1 } else { 0 }) + .collect(); + let mut cnt: HashMap = HashMap::new(); + cnt.insert(0, 1); + + let mut ans: i64 = 0; + let mut s: i32 = 0; + + for x in arr { + s += x; + let key = (s - k).rem_euclid(modulo); + ans += *cnt.get(&key).unwrap_or(&0); + *cnt.entry(s % modulo).or_insert(0) += 1; + } + + ans + } +} +``` +