File tree Expand file tree Collapse file tree 6 files changed +206
-2
lines changed
solution/1500-1599/1524.Number of Sub-arrays With Odd Sum Expand file tree Collapse file tree 6 files changed +206
-2
lines changed Original file line number Diff line number Diff line change 59
59
60
60
## 解法
61
61
62
+ 前缀和 + 计数器。
63
+
62
64
<!-- 这里可写通用的实现逻辑 -->
63
65
64
66
<!-- tabs:start -->
68
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
71
70
72
``` 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
72
86
```
73
87
74
88
### ** Java**
75
89
76
90
<!-- 这里可写当前语言的特殊实现逻辑 -->
77
91
78
92
``` 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
+ ```
79
133
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
+ }
80
152
```
81
153
82
154
### ** ...**
Original file line number Diff line number Diff line change @@ -88,13 +88,83 @@ All sub-arrays have even sum and the answer is 0.
88
88
### ** Python3**
89
89
90
90
``` 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
92
104
```
93
105
94
106
### ** Java**
95
107
96
108
``` 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
+ ```
97
149
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
+ }
98
168
```
99
169
100
170
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments