力扣第1248题 统计「优美子数组」
class Solution {
public:
int numberOfSubarrays(vector<int>& nums, int k)
{
int len = nums.size();
vector<int> cnt(len + 1, 0);
int res = 0, odd = 0;
cnt[0] = 1;
for (int i = 0; i < len; i++)
{
odd += nums[i] & 1;
res += odd >= k ? cnt[odd - k] : 0;
cnt[odd] += 1;
}
return res;
}
};