class Solution {
public double findMaxAverage(int[] nums, int k) {
int sum = 0, len = nums.length;
for(int i = 0; i < k; i++) sum += nums[i];
int max = sum;
for(int i = k; i < len; i++){
sum -= nums[i - k] - nums[i];
max = Math.max(sum, max);
}
return (double)max/k;
}
}
643.子数最大平均数Ⅰ。滑动窗口
最新推荐文章于 2024-11-01 15:19:21 发布