力扣第1248题 统计「优美子数组」

力扣第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;
    }
};

posted on 2020-04-23 23:39  woodjay  阅读(97)  评论(0编辑  收藏  举报

导航