Skip to content

Commit 4a32c1c

Browse files
authored
feat: add solutions to lc problem: No.1524 (#4112)
No.1524.Number of Sub-arrays With Odd Sum
1 parent c9cd048 commit 4a32c1c

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README.md

+22-3
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ tags:
8080

8181
### 方法一:前缀和 + 计数器
8282

83-
我们定义一个长度为 $2$ 的数组 $cnt$ 作为计数器,其中 $cnt[0]$ 和 $cnt[1]$ 分别表示前缀和为偶数和奇数的子数组的个数。初始时 $cnt[0] = 1$,而 $cnt[1] = 0$。
83+
我们定义一个长度为 $2$ 的数组 $\textit{cnt}$ 作为计数器,其中 $\textit{cnt}[0]$ 和 $\textit{cnt}[1]$ 分别表示前缀和为偶数和奇数的子数组的个数。初始时 $\textit{cnt}[0] = 1$,而 $\textit{cnt}[1] = 0$。
8484

8585
接下来,我们维护当前的前缀和 $s$,初始时 $s = 0$。
8686

87-
遍历数组 $arr$,对于遍历到的每个元素 $x$,我们将 $s$ 的值加上 $x$,然后根据 $s$ 的奇偶性,将 $cnt[s \mod 2 \oplus 1]$ 的值累加到答案中,然后我们将 $cnt[s \mod 2]$ 的值加 $1$。
87+
遍历数组 $\textit{arr}$,对于遍历到的每个元素 $x$,我们将 $s$ 的值加上 $x$,然后根据 $s$ 的奇偶性,将 $\textit{cnt}[s \mod 2 \oplus 1]$ 的值累加到答案中,然后我们将 $\textit{cnt}[s \mod 2]$ 的值加 $1$。
8888

8989
遍历结束后,我们即可得到答案。注意答案的取模运算。
9090

91-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。
91+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{arr}$ 的长度。空间复杂度 $O(1)$
9292

9393
<!-- tabs:start -->
9494

@@ -177,6 +177,25 @@ function numOfSubarrays(arr: number[]): number {
177177
}
178178
```
179179

180+
#### Rust
181+
182+
```rust
183+
impl Solution {
184+
pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {
185+
const MOD: i32 = 1_000_000_007;
186+
let mut cnt = [1, 0];
187+
let mut ans = 0;
188+
let mut s = 0;
189+
for &x in arr.iter() {
190+
s += x;
191+
ans = (ans + cnt[((s & 1) ^ 1) as usize]) % MOD;
192+
cnt[(s & 1) as usize] += 1;
193+
}
194+
ans
195+
}
196+
}
197+
```
198+
180199
<!-- tabs:end -->
181200

182201
<!-- solution:end -->

solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README_EN.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ All sub-arrays have even sum and the answer is 0.
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Prefix Sum + Counter
71+
72+
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$.
73+
74+
Next, we maintain the current prefix sum $s$, initially $s = 0$.
75+
76+
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.
77+
78+
After the traversal, we get the answer. Note the modulo operation for the answer.
79+
80+
Time complexity is $O(n)$, where $n$ is the length of the array $\textit{arr}$. Space complexity is $O(1)$.
7181

7282
<!-- tabs:start -->
7383

@@ -156,6 +166,25 @@ function numOfSubarrays(arr: number[]): number {
156166
}
157167
```
158168

169+
#### Rust
170+
171+
```rust
172+
impl Solution {
173+
pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {
174+
const MOD: i32 = 1_000_000_007;
175+
let mut cnt = [1, 0];
176+
let mut ans = 0;
177+
let mut s = 0;
178+
for &x in arr.iter() {
179+
s += x;
180+
ans = (ans + cnt[((s & 1) ^ 1) as usize]) % MOD;
181+
cnt[(s & 1) as usize] += 1;
182+
}
183+
ans
184+
}
185+
}
186+
```
187+
159188
<!-- tabs:end -->
160189

161190
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {
3+
const MOD: i32 = 1_000_000_007;
4+
let mut cnt = [1, 0];
5+
let mut ans = 0;
6+
let mut s = 0;
7+
for &x in arr.iter() {
8+
s += x;
9+
ans = (ans + cnt[((s & 1) ^ 1) as usize]) % MOD;
10+
cnt[(s & 1) as usize] += 1;
11+
}
12+
ans
13+
}
14+
}

0 commit comments

Comments
 (0)