classSolution{ publicdoublefindMaxAverage(int[] nums, int k){ //先算出第一组的平均数 int sum = 0; for(int i=0;i<k;i++){ sum += nums[i]; } double max = (double)sum/k; for(int i=k;i<nums.length;i++){ //计算窗口内的值,即i-k+1到i之间的数字,一直往后滑动 sum -= nums[i-k]; sum += nums[i]; double res = (double)sum/k; if(res > max){ max = res; } } return max; } }