Skip to content

Commit be4e073

Browse files
committed
Runtime: 13 ms (Top 83.33%) | Memory: 2.30 MB (Top 100.0%)
1 parent 70af55c commit be4e073

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Runtime: 13 ms (Top 83.33%) | Memory: 2.30 MB (Top 100.0%)
2+
3+
impl Solution {
4+
pub fn number_of_subarrays(nums: Vec<i32>, k: i32) -> i32 {
5+
let mut total = 0;
6+
let mut odd = 0;
7+
let mut count = 0;
8+
9+
let mut j = 0;
10+
let is_odd = |x| x % 2 == 1;
11+
12+
for &num in nums.iter() {
13+
if is_odd(num) {
14+
odd += 1;
15+
count = 0;
16+
}
17+
18+
while odd == k {
19+
if is_odd(nums[j]) {
20+
odd -= 1;
21+
}
22+
count += 1;
23+
j += 1;
24+
}
25+
total += count;
26+
}
27+
28+
total as i32
29+
}
30+
}
31+

0 commit comments

Comments
 (0)