Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4a32c1c

Browse files
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‎

Lines changed: 22 additions & 3 deletions
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‎

Lines changed: 30 additions & 1 deletion
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 -->
Lines changed: 14 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /