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
| class Solution { public: int numberOfSubarrays(vector<int>& nums, int k) { int n = nums.size(); if (n < k) return 0;
int left = 0, right = 0, ans = 0; int oddcount = nums[0] % 2 == 1 ? 1 : 0; int leftcount = 0, rightcount = 0; while (left < n) { if (oddcount == k) { while (right < n - 1 && nums[right + 1] % 2 == 0) { right++; rightcount++; } while (left <= right && left < n - 1 && nums[left] % 2 == 0) { left++; leftcount++; } ans += (leftcount + 1) * (rightcount + 1); leftcount = 0; rightcount = 0;
if (right < n - 1) { right++; oddcount++; } if (left <= right && left < n - 1) { left++; oddcount--; } else if (left == n - 1) break; }
if (oddcount < k && right < n - 1) { right++; if (nums[right] % 2 == 1) oddcount++; } else if (oddcount < k && right >= n - 1) break; }
return ans; } };
|