题解 | 滑动窗口的最大值
滑动窗口的最大值
https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @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 ) { if(numLen==0||size==0)return NULL; int left = 0, right = size - 1;//窗口左右边 int max = num[0]; *returnSize = numLen - size + 1; int *res = (int*)malloc(sizeof(int)*(*returnSize)); int k = 0; while(right < numLen){ for(int i=left;i<=right;i++){ if(max<num[i])max=num[i]; } res[k++] = max; left++;right++; max=num[left]; } return res; }
暴力求解