Skip to content

Commit 6554458

Browse files
committed
feat: add solutions to lc problem: No.1524
No.1524.Number of Sub-arrays With Odd Sum
1 parent 952eb6b commit 6554458

File tree

7 files changed

+151
-140
lines changed

7 files changed

+151
-140
lines changed

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

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,20 @@
5959

6060
## 解法
6161

62-
前缀和 + 计数器。
63-
6462
<!-- 这里可写通用的实现逻辑 -->
6563

64+
**方法一:前缀和 + 计数器**
65+
66+
我们定义一个长度为 $2$ 的数组 $cnt$ 作为计数器,其中 $cnt[0]$ 和 $cnt[1]$ 分别表示前缀和为偶数和奇数的子数组的个数。初始时,$cnt[0] = 1$,而 $cnt[1] = 0$。
67+
68+
接下来,我们维护当前的前缀和 $s$,初始时 $s = 0$。
69+
70+
遍历数组 $arr$,对于遍历到的每个元素 $x$,我们将 $s$ 的值加上 $x$,然后根据 $s$ 的奇偶性,将 $cnt[s \& 1 \oplus 1]$ 的值累加到答案中,然后我们将 $cnt[s \& 1]$ 的值加 $1$。
71+
72+
遍历结束后,我们即可得到答案。注意答案的取模运算。
73+
74+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。
75+
6676
<!-- tabs:start -->
6777

6878
### **Python3**
@@ -72,17 +82,14 @@
7282
```python
7383
class Solution:
7484
def numOfSubarrays(self, arr: List[int]) -> int:
75-
MOD = int(1e9) + 7
76-
counter = [0] * 2
77-
s = ans = 0
78-
for v in arr:
79-
s += v
80-
counter[s % 2] += 1
81-
if s % 2 == 1:
82-
ans += 1 + counter[0]
83-
else:
84-
ans += counter[1]
85-
return ans % MOD
85+
mod = 10**9 + 7
86+
cnt = [1, 0]
87+
ans = s = 0
88+
for x in arr:
89+
s += x
90+
ans = (ans + cnt[s & 1 ^ 1]) % mod
91+
cnt[s & 1] += 1
92+
return ans
8693
```
8794

8895
### **Java**
@@ -91,19 +98,14 @@ class Solution:
9198

9299
```java
93100
class Solution {
94-
private static final int MOD = (int) 1e9 + 7;
95-
96101
public int numOfSubarrays(int[] arr) {
97-
int[] counter = new int[2];
98-
int s = 0, ans = 0;
99-
for (int v : arr) {
100-
s += v;
101-
++counter[s % 2];
102-
if (s % 2 == 1) {
103-
ans = (ans + 1 + counter[0]) % MOD;
104-
} else {
105-
ans = (ans + counter[1]) % MOD;
106-
}
102+
final int mod = (int) 1e9 + 7;
103+
int[] cnt = {1, 0};
104+
int ans = 0, s = 0;
105+
for (int x : arr) {
106+
s += x;
107+
ans = (ans + cnt[s & 1 ^ 1]) % mod;
108+
++cnt[s & 1];
107109
}
108110
return ans;
109111
}
@@ -116,16 +118,13 @@ class Solution {
116118
class Solution {
117119
public:
118120
int numOfSubarrays(vector<int>& arr) {
119-
const int MOD = 1e9 + 7;
120-
vector<int> counter(2);
121-
int s = 0, ans = 0;
122-
for (int& v : arr) {
123-
s += v;
124-
++counter[s % 2];
125-
if (s % 2 == 1)
126-
ans = (ans + 1 + counter[0]) % MOD;
127-
else
128-
ans = (ans + counter[1]) % MOD;
121+
const int mod = 1e9 + 7;
122+
int cnt[2] = {1, 0};
123+
int ans = 0, s = 0;
124+
for (int x : arr) {
125+
s += x;
126+
ans = (ans + cnt[s & 1 ^ 1]) % mod;
127+
++cnt[s & 1];
129128
}
130129
return ans;
131130
}
@@ -135,20 +134,33 @@ public:
135134
### **Go**
136135
137136
```go
138-
func numOfSubarrays(arr []int) int {
139-
const MOD = 1e9 + 7
140-
counter := make([]int, 2)
141-
s, ans := 0, 0
142-
for _, v := range arr {
143-
s += v
144-
counter[s%2]++
145-
if s%2 == 1 {
146-
ans = (ans + 1 + counter[0]) % MOD
147-
} else {
148-
ans = (ans + counter[1]) % MOD
149-
}
137+
func numOfSubarrays(arr []int) (ans int) {
138+
const mod int = 1e9 + 7
139+
cnt := [2]int{1, 0}
140+
s := 0
141+
for _, x := range arr {
142+
s += x
143+
ans = (ans + cnt[s&1^1]) % mod
144+
cnt[s&1]++
150145
}
151-
return ans
146+
return
147+
}
148+
```
149+
150+
### **TypeScript**
151+
152+
```ts
153+
function numOfSubarrays(arr: number[]): number {
154+
let ans = 0;
155+
let s = 0;
156+
const cnt: number[] = [1, 0];
157+
const mod = 1e9 + 7;
158+
for (const x of arr) {
159+
s += x;
160+
ans = (ans + cnt[(s & 1) ^ 1]) % mod;
161+
cnt[s & 1]++;
162+
}
163+
return ans;
152164
}
153165
```
154166

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

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,28 @@ All sub-arrays have even sum and the answer is 0.
5353
```python
5454
class Solution:
5555
def numOfSubarrays(self, arr: List[int]) -> int:
56-
MOD = int(1e9) + 7
57-
counter = [0] * 2
58-
s = ans = 0
59-
for v in arr:
60-
s += v
61-
counter[s % 2] += 1
62-
if s % 2 == 1:
63-
ans += 1 + counter[0]
64-
else:
65-
ans += counter[1]
66-
return ans % MOD
56+
mod = 10**9 + 7
57+
cnt = [1, 0]
58+
ans = s = 0
59+
for x in arr:
60+
s += x
61+
ans = (ans + cnt[s & 1 ^ 1]) % mod
62+
cnt[s & 1] += 1
63+
return ans
6764
```
6865

6966
### **Java**
7067

7168
```java
7269
class Solution {
73-
private static final int MOD = (int) 1e9 + 7;
74-
7570
public int numOfSubarrays(int[] arr) {
76-
int[] counter = new int[2];
77-
int s = 0, ans = 0;
78-
for (int v : arr) {
79-
s += v;
80-
++counter[s % 2];
81-
if (s % 2 == 1) {
82-
ans = (ans + 1 + counter[0]) % MOD;
83-
} else {
84-
ans = (ans + counter[1]) % MOD;
85-
}
71+
final int mod = (int) 1e9 + 7;
72+
int[] cnt = {1, 0};
73+
int ans = 0, s = 0;
74+
for (int x : arr) {
75+
s += x;
76+
ans = (ans + cnt[s & 1 ^ 1]) % mod;
77+
++cnt[s & 1];
8678
}
8779
return ans;
8880
}
@@ -95,16 +87,13 @@ class Solution {
9587
class Solution {
9688
public:
9789
int numOfSubarrays(vector<int>& arr) {
98-
const int MOD = 1e9 + 7;
99-
vector<int> counter(2);
100-
int s = 0, ans = 0;
101-
for (int& v : arr) {
102-
s += v;
103-
++counter[s % 2];
104-
if (s % 2 == 1)
105-
ans = (ans + 1 + counter[0]) % MOD;
106-
else
107-
ans = (ans + counter[1]) % MOD;
90+
const int mod = 1e9 + 7;
91+
int cnt[2] = {1, 0};
92+
int ans = 0, s = 0;
93+
for (int x : arr) {
94+
s += x;
95+
ans = (ans + cnt[s & 1 ^ 1]) % mod;
96+
++cnt[s & 1];
10897
}
10998
return ans;
11099
}
@@ -114,20 +103,33 @@ public:
114103
### **Go**
115104
116105
```go
117-
func numOfSubarrays(arr []int) int {
118-
const MOD = 1e9 + 7
119-
counter := make([]int, 2)
120-
s, ans := 0, 0
121-
for _, v := range arr {
122-
s += v
123-
counter[s%2]++
124-
if s%2 == 1 {
125-
ans = (ans + 1 + counter[0]) % MOD
126-
} else {
127-
ans = (ans + counter[1]) % MOD
128-
}
106+
func numOfSubarrays(arr []int) (ans int) {
107+
const mod int = 1e9 + 7
108+
cnt := [2]int{1, 0}
109+
s := 0
110+
for _, x := range arr {
111+
s += x
112+
ans = (ans + cnt[s&1^1]) % mod
113+
cnt[s&1]++
129114
}
130-
return ans
115+
return
116+
}
117+
```
118+
119+
### **TypeScript**
120+
121+
```ts
122+
function numOfSubarrays(arr: number[]): number {
123+
let ans = 0;
124+
let s = 0;
125+
const cnt: number[] = [1, 0];
126+
const mod = 1e9 + 7;
127+
for (const x of arr) {
128+
s += x;
129+
ans = (ans + cnt[(s & 1) ^ 1]) % mod;
130+
cnt[s & 1]++;
131+
}
132+
return ans;
131133
}
132134
```
133135

solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/Solution.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
class Solution {
22
public:
33
int numOfSubarrays(vector<int>& arr) {
4-
const int MOD = 1e9 + 7;
5-
vector<int> counter(2);
6-
int s = 0, ans = 0;
7-
for (int& v : arr) {
8-
s += v;
9-
++counter[s % 2];
10-
if (s % 2 == 1)
11-
ans = (ans + 1 + counter[0]) % MOD;
12-
else
13-
ans = (ans + counter[1]) % MOD;
4+
const int mod = 1e9 + 7;
5+
int cnt[2] = {1, 0};
6+
int ans = 0, s = 0;
7+
for (int x : arr) {
8+
s += x;
9+
ans = (ans + cnt[s & 1 ^ 1]) % mod;
10+
++cnt[s & 1];
1411
}
1512
return ans;
1613
}
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
func numOfSubarrays(arr []int) int {
2-
const MOD = 1e9 + 7
3-
counter := make([]int, 2)
4-
s, ans := 0, 0
5-
for _, v := range arr {
6-
s += v
7-
counter[s%2]++
8-
if s%2 == 1 {
9-
ans = (ans + 1 + counter[0]) % MOD
10-
} else {
11-
ans = (ans + counter[1]) % MOD
12-
}
1+
func numOfSubarrays(arr []int) (ans int) {
2+
const mod int = 1e9 + 7
3+
cnt := [2]int{1, 0}
4+
s := 0
5+
for _, x := range arr {
6+
s += x
7+
ans = (ans + cnt[s&1^1]) % mod
8+
cnt[s&1]++
139
}
14-
return ans
10+
return
1511
}

solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/Solution.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
class Solution {
2-
private static final int MOD = (int) 1e9 + 7;
3-
42
public int numOfSubarrays(int[] arr) {
5-
int[] counter = new int[2];
6-
int s = 0, ans = 0;
7-
for (int v : arr) {
8-
s += v;
9-
++counter[s % 2];
10-
if (s % 2 == 1) {
11-
ans = (ans + 1 + counter[0]) % MOD;
12-
} else {
13-
ans = (ans + counter[1]) % MOD;
14-
}
3+
final int mod = (int) 1e9 + 7;
4+
int[] cnt = {1, 0};
5+
int ans = 0, s = 0;
6+
for (int x : arr) {
7+
s += x;
8+
ans = (ans + cnt[s & 1 ^ 1]) % mod;
9+
++cnt[s & 1];
1510
}
1611
return ans;
1712
}
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
class Solution:
22
def numOfSubarrays(self, arr: List[int]) -> int:
3-
MOD = int(1e9) + 7
4-
counter = [0] * 2
5-
s = ans = 0
6-
for v in arr:
7-
s += v
8-
counter[s % 2] += 1
9-
if s % 2 == 1:
10-
ans += 1 + counter[0]
11-
else:
12-
ans += counter[1]
13-
return ans % MOD
3+
mod = 10**9 + 7
4+
cnt = [1, 0]
5+
ans = s = 0
6+
for x in arr:
7+
s += x
8+
ans = (ans + cnt[s & 1 ^ 1]) % mod
9+
cnt[s & 1] += 1
10+
return ans
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function numOfSubarrays(arr: number[]): number {
2+
let ans = 0;
3+
let s = 0;
4+
const cnt: number[] = [1, 0];
5+
const mod = 1e9 + 7;
6+
for (const x of arr) {
7+
s += x;
8+
ans = (ans + cnt[(s & 1) ^ 1]) % mod;
9+
cnt[s & 1]++;
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)