diff --git a/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README.md b/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README.md index 97fbd0514a02f..ade0242a435e8 100644 --- a/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README.md +++ b/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README.md @@ -80,15 +80,15 @@ tags: ### 方法一:前缀和 + 计数器 -我们定义一个长度为 $2$ 的数组 $cnt$ 作为计数器,其中 $cnt[0]$ 和 $cnt[1]$ 分别表示前缀和为偶数和奇数的子数组的个数。初始时 $cnt[0] = 1$,而 $cnt[1] = 0$。 +我们定义一个长度为 $2$ 的数组 $\textit{cnt}$ 作为计数器,其中 $\textit{cnt}[0]$ 和 $\textit{cnt}[1]$ 分别表示前缀和为偶数和奇数的子数组的个数。初始时 $\textit{cnt}[0] = 1$,而 $\textit{cnt}[1] = 0$。 接下来,我们维护当前的前缀和 $s$,初始时 $s = 0$。 -遍历数组 $arr$,对于遍历到的每个元素 $x$,我们将 $s$ 的值加上 $x$,然后根据 $s$ 的奇偶性,将 $cnt[s \mod 2 \oplus 1]$ 的值累加到答案中,然后我们将 $cnt[s \mod 2]$ 的值加 $1$。 +遍历数组 $\textit{arr}$,对于遍历到的每个元素 $x$,我们将 $s$ 的值加上 $x$,然后根据 $s$ 的奇偶性,将 $\textit{cnt}[s \mod 2 \oplus 1]$ 的值累加到答案中,然后我们将 $\textit{cnt}[s \mod 2]$ 的值加 $1$。 遍历结束后,我们即可得到答案。注意答案的取模运算。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{arr}$ 的长度。空间复杂度 $O(1)$。 @@ -177,6 +177,25 @@ function numOfSubarrays(arr: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn num_of_subarrays(arr: Vec) -> i32 { + const MOD: i32 = 1_000_000_007; + let mut cnt = [1, 0]; + let mut ans = 0; + let mut s = 0; + for &x in arr.iter() { + s += x; + ans = (ans + cnt[((s & 1) ^ 1) as usize]) % MOD; + cnt[(s & 1) as usize] += 1; + } + ans + } +} +``` + diff --git a/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README_EN.md b/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README_EN.md index 5cf6739dc03d8..901fd189ef2d8 100644 --- a/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README_EN.md +++ b/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README_EN.md @@ -67,7 +67,17 @@ All sub-arrays have even sum and the answer is 0. -### Solution 1 +### Solution 1: Prefix Sum + Counter + +We define an array $\textit{cnt}$ of length 2 as a counter, where $\textit{cnt}[0]$ and $\textit{cnt}[1]$ represent the number of subarrays with even and odd prefix sums, respectively. Initially, $\textit{cnt}[0] = 1$ and $\textit{cnt}[1] = 0$. + +Next, we maintain the current prefix sum $s$, initially $s = 0$. + +Traverse the array $\textit{arr}$, for each element $x$ encountered, add the value of $x$ to $s$, then based on the parity of $s$, add the value of $\textit{cnt}[s \mod 2 \oplus 1]$ to the answer, and then increment the value of $\textit{cnt}[s \mod 2]$ by 1. + +After the traversal, we get the answer. Note the modulo operation for the answer. + +Time complexity is $O(n)$, where $n$ is the length of the array $\textit{arr}$. Space complexity is $O(1)$. @@ -156,6 +166,25 @@ function numOfSubarrays(arr: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn num_of_subarrays(arr: Vec) -> i32 { + const MOD: i32 = 1_000_000_007; + let mut cnt = [1, 0]; + let mut ans = 0; + let mut s = 0; + for &x in arr.iter() { + s += x; + ans = (ans + cnt[((s & 1) ^ 1) as usize]) % MOD; + cnt[(s & 1) as usize] += 1; + } + ans + } +} +``` + diff --git a/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/Solution.rs b/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/Solution.rs new file mode 100644 index 0000000000000..72c387ec66a85 --- /dev/null +++ b/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/Solution.rs @@ -0,0 +1,14 @@ +impl Solution { + pub fn num_of_subarrays(arr: Vec) -> i32 { + const MOD: i32 = 1_000_000_007; + let mut cnt = [1, 0]; + let mut ans = 0; + let mut s = 0; + for &x in arr.iter() { + s += x; + ans = (ans + cnt[((s & 1) ^ 1) as usize]) % MOD; + cnt[(s & 1) as usize] += 1; + } + ans + } +}