public int[] maxSlidingWindow(int[] nums, int k) { int len = nums.length; if(len == 0 ) return new int[0]; int[] res = new int[len-k+1]; int max = nums[0]; int index = 0; for(int i = 1;i<k;i++){ if(nums[i] > max){ max = nums[i]; index = i; } } int index_res = 0; int j = k; int i = 0; res[index_res++] = max; while(j<len){ if(nums[j] >= max){ max = nums[j]; index = j; i++; }else{ if(index == i){ i++; //重新找从i到j之间的最大值 max = nums[i]; index = i; for(int t = i;t<=j;t++){ if(nums[t] > max){ max = nums[t]; index = i; } } }else{ i++; } } res[index_res++] = max; j++; } return res; }