LeetCode 2760. 最长奇偶子数组 C#
原题链接
简单
作者:
hpstory
,
2023-07-03 13:58:08
,
所有人可见
,
阅读 69
C# 代码
public class Solution {
public int LongestAlternatingSubarray(int[] nums, int threshold) {
int n = nums.Length, result = 0;
Dictionary<int, int> dict = new Dictionary<int, int>();
for (int i = 0; i < n; i++){
if (nums[i] % 2 == 0 && nums[i] <= threshold){
int j = i + 1;
while (j < n && nums[j] % 2 != nums[j - 1] % 2 && nums[j] <= threshold) j++;
result = Math.Max(result, j - i);
i = j - 1;
}
}
return result;
}
}