题解 | #滑动窗口的最大值#
滑动窗口的最大值
https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
//比较大小所用的函数 int max(int* numList, int numSize) { int max = numList[0]; for (int i = 1; i < numSize; i++) { if (numList[i] > max) { max = numList[i]; } } return max; } //参数四:结果数组的长度 int* maxInWindows(int* num, int numLen, int size, int* returnSize ) { if (!size || size > numLen) return NULL; *returnSize = 0; int* dest = (int*)malloc(sizeof(int) * (numLen - size + 1)); int head = 0, tail = 0; //遍历num for (int i = 0; i <= numLen; i++) { if (tail - head == size) { dest[(*returnSize)++] = max(&num[head], size); head++; } if (tail - head < size) { tail++; } } return dest; }