643. 子数组最大平均数 I
分类:滑动窗口
维护下窗口长度的子数组和就行
class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
nums.push_back(0); //不会对结果产生影响,但是方便循环
int maxsum = -3e8, i=0, j=0, currentsum=0;
while(j < k) currentsum += nums[j++];
for(; j<nums.size(); j++, i++){
maxsum = max(maxsum, currentsum);
currentsum-=nums[i];
currentsum+=nums[j];
}
return maxsum*1.0/k;
}
};/* 84.9% **/
2021/02/04