/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型一维数组 * @param numLen int num数组长度 * @param size int整型 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* maxInWindows(int* num, int numLen, int size, int* returnSize ) { // write code here if(size == 0 || size > numLen) { *returnSize = 0; return NULL; } int *max = (int*)malloc(sizeof(int) * (numLen - size + 1)); (*returnSize) = -1; int *inc = (int*)malloc(sizeof(int) * numLen); int left = 0, right = 0; int numIdx = 1; inc[0] = 0; while(numIdx < size) { printf("当前滑动窗口的索引值范围是%d-%d\n", numIdx - size + 1, numIdx); printf("正在查看数字%d\n", num[numIdx]); while(right >= left && num[inc[right]] < num[numIdx]) { printf("%d小于%d,右索引向左移\n",num[inc[right]],num[numIdx]); right--; } inc[++right] = numIdx++; printf("当前左索引%d,右索引%d\n", left, right); printf("左索引值为%d,右索引值为%d\n", num[inc[left]], num[inc[right]]); printf("当前numIdx:%d\n",numIdx); } (*returnSize) = 0; max[*returnSize] = num[inc[left]]; printf("将滑动窗口中的最大值%d放入max数组中\n", num[inc[left]]); (*returnSize)++; while(numIdx < numLen) { printf("当前滑动窗口的索引值范围是%d-%d\n", numIdx - size + 1, numIdx); printf("正在查看数字%d\n", num[numIdx]); while(right >= left && num[inc[right]] < num[numIdx]) { right--; printf("%d小于%d,右索引向左移\n",num[inc[right + 1]],num[numIdx]); } inc[++right] = numIdx++; while(inc[left] < numIdx - size) { left++; printf("最左侧的索引值%d超出了滑动窗口的范围,当前滑动窗口的索引值范围是%d-%d\n", inc[left], numIdx - 1 - size, numIdx - 1); } printf("当前左索引%d,右索引%d\n", left, right); printf("左索引值为%d,右索引值为%d\n", num[inc[left]], num[inc[right]]); max[*returnSize] = num[inc[left]]; printf("将滑动窗口中的最大值%d放入max数组中\n", num[inc[left]]); (*returnSize)++; } return max; }