题目描述如上,我写的代码如下
int longestAlternatingSubarray(int* nums, int numsSize, int threshold) {
int longsize = 0;
int l = numsSize;
int s = 0;
for (int i = 0; i < numsSize; i++)
{
if (nums[i] % 2 == 0&&nums[i]<=threshold)
{
l = i;
break;
}
}
if (l == numsSize)
return 0;
while (nums[l] == nums[numsSize - 1])
{
if (nums[l] % 2 == 0 && nums[l] <= threshold)
return 1;
else
return 0;
}
for (int i = l; i < numsSize - 1; i++)
{
if (nums[i] % 2 != nums[i + 1] % 2 && nums[i] <= threshold)
{
longsize++;
s = i;
}
}
if (nums[s + 1] <= threshold)
{
longsize++;
}
return longsize;
}
测试不通过的例子如下
最开始找出的下标l应该是0,然后因为mod2不等于mod8,而且threshold=4,8>4,按理说数组中没有满足条件的值,为什么官方给出的预期结果是1?可能是我对题目的理解不到位,希望解答,感谢