A subarray is a contiguous portion of an array. Given an array of integers, you must determine the number of distinct subarrays that can be formed having at most a given number of odd elements. Two subarrays are distinct if they differ at even one position in their contents.
For example, if numbers = [1, 2, 3, 4] and the maximum number of odd elements allowed, k = 1, the following is a list of the 8 distinct valid subarrays:
[[1], [2], [3], [4], [1,2], [2, 3], [3, 4], [2, 3, 4]]
Function Description
Complete the function evenSubarray in the editor below. The function must return the number of distinct subarrays that can be formed per the restriction of k.
evenSubarray has the following parameter(s):
numbersThumbers[0],...numbers[n-1]]: an array of integers
k: the maximum number of odd elements that can be in a subarray
题意:
给定数组,返回包含最多K个奇数的subarray的个数
思路:
代码:
原文地址:https://www.cnblogs.com/liuliu5151/p/11509812.html