Skip to content

Commit 0b01539

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

File tree

6 files changed

+206
-2
lines changed

6 files changed

+206
-2
lines changed

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959

6060
## 解法
6161

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

6466
<!-- tabs:start -->
@@ -68,15 +70,85 @@
6870
<!-- 这里可写当前语言的特殊实现逻辑 -->
6971

7072
```python
71-
73+
class Solution:
74+
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
7286
```
7387

7488
### **Java**
7589

7690
<!-- 这里可写当前语言的特殊实现逻辑 -->
7791

7892
```java
93+
class Solution {
94+
private static final int MOD = (int) 1e9 + 7;
95+
96+
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+
}
107+
}
108+
return ans;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
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+
{
124+
s += v;
125+
++counter[s % 2];
126+
if (s % 2 == 1) ans = (ans + 1 + counter[0]) % MOD;
127+
else ans = (ans + counter[1]) % MOD;
128+
}
129+
return ans;
130+
}
131+
};
132+
```
79133
134+
### **Go**
135+
136+
```go
137+
func numOfSubarrays(arr []int) int {
138+
const MOD = 1e9 + 7
139+
counter := make([]int, 2)
140+
s, ans := 0, 0
141+
for _, v := range arr {
142+
s += v
143+
counter[s%2]++
144+
if s%2 == 1 {
145+
ans = (ans + 1 + counter[0]) % MOD
146+
} else {
147+
ans = (ans + counter[1]) % MOD
148+
}
149+
}
150+
return ans
151+
}
80152
```
81153

82154
### **...**

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,83 @@ All sub-arrays have even sum and the answer is 0.
8888
### **Python3**
8989

9090
```python
91-
91+
class Solution:
92+
def numOfSubarrays(self, arr: List[int]) -> int:
93+
MOD = int(1e9) + 7
94+
counter = [0] * 2
95+
s = ans = 0
96+
for v in arr:
97+
s += v
98+
counter[s % 2] += 1
99+
if s % 2 == 1:
100+
ans += 1 + counter[0]
101+
else:
102+
ans += counter[1]
103+
return ans % MOD
92104
```
93105

94106
### **Java**
95107

96108
```java
109+
class Solution {
110+
private static final int MOD = (int) 1e9 + 7;
111+
112+
public int numOfSubarrays(int[] arr) {
113+
int[] counter = new int[2];
114+
int s = 0, ans = 0;
115+
for (int v : arr) {
116+
s += v;
117+
++counter[s % 2];
118+
if (s % 2 == 1) {
119+
ans = (ans + 1 + counter[0]) % MOD;
120+
} else {
121+
ans = (ans + counter[1]) % MOD;
122+
}
123+
}
124+
return ans;
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
int numOfSubarrays(vector<int>& arr) {
135+
const int MOD = 1e9 + 7;
136+
vector<int> counter(2);
137+
int s = 0, ans = 0;
138+
for (int& v : arr)
139+
{
140+
s += v;
141+
++counter[s % 2];
142+
if (s % 2 == 1) ans = (ans + 1 + counter[0]) % MOD;
143+
else ans = (ans + counter[1]) % MOD;
144+
}
145+
return ans;
146+
}
147+
};
148+
```
97149
150+
### **Go**
151+
152+
```go
153+
func numOfSubarrays(arr []int) int {
154+
const MOD = 1e9 + 7
155+
counter := make([]int, 2)
156+
s, ans := 0, 0
157+
for _, v := range arr {
158+
s += v
159+
counter[s%2]++
160+
if s%2 == 1 {
161+
ans = (ans + 1 + counter[0]) % MOD
162+
} else {
163+
ans = (ans + counter[1]) % MOD
164+
}
165+
}
166+
return ans
167+
}
98168
```
99169

100170
### **...**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
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+
{
9+
s += v;
10+
++counter[s % 2];
11+
if (s % 2 == 1) ans = (ans + 1 + counter[0]) % MOD;
12+
else ans = (ans + counter[1]) % MOD;
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
13+
}
14+
return ans
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
private static final int MOD = (int) 1e9 + 7;
3+
4+
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+
}
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
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

0 commit comments

Comments
 (0)