Problem: 2760. 最长奇偶子数组
每日一题。实习第10天记录。
思路
注意用条件找r。
Code
class Solution {
public int longestAlternatingSubarray(int[] nums, int threshold) {
int len = nums.length;
int l, r;
int res = 0;
for (l = 0; l < len; l++) {
// 定位l
if (nums[l] % 2 == 0 && nums[l] <= threshold) {
// 定位r
r = l + 1;
while (r < len && nums[r] % 2 != nums[r - 1] % 2 && nums[r] <= threshold) {
r++;
}
res = Math.max(res, r - l);
}
}
return res;
}
}