643.子数组最大平均数I
方法:滑动窗口
class Solution {
public double findMaxAverage(int[] nums, int k) {
int n = nums.length;
int winSum = 0;
//先求出第一个窗口的和
for(int i = 0;i<k;i++){
winSum += nums[i];
}
//通过遍历求出除了第一窗口的和
int res = winSum;
for(int i = k;i<n;i++){
winSum = winSum + nums[i] - nums[i - k];
res = Math.max(winSum,res);
}
return (double) res / k;
}
}