-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcount-number-of-nice-subarrays.rs
51 lines (43 loc) · 1.3 KB
/
count-number-of-nice-subarrays.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#![allow(dead_code, unused, unused_variables)]
fn main() {
println!(
"{}",
Solution::number_of_subarrays(vec![2, 2, 2, 1, 2, 2, 1, 2, 2, 2], 2)
);
println!(
"{}",
Solution::number_of_subarrays(vec![2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1], 2)
);
}
struct Solution;
impl Solution {
pub fn number_of_subarrays(nums: Vec<i32>, k: i32) -> i32 {
let mut count = 0usize;
let mut index_list: Vec<usize> = vec![]; // 用于存放奇数元素索引的数组
let k = k as usize;
for (index, &value) in nums.iter().enumerate() {
if value % 2 == 1 {
index_list.push(index);
}
}
if index_list.len() < k {
return count as i32;
}
for (index, &value) in index_list.iter().skip(k - 1).enumerate() {
let mut s = 0;
let mut t = 0;
if index == 0 {
s = index_list[0] + 1;
} else {
s = index_list[index] - index_list[index - 1];
}
if index + k == index_list.len() {
t = nums.len() - value;
} else {
t = index_list[index + k] - index_list[index + k - 1];
}
count += s * t;
}
count as i32
}
}