643.子数组最大平均数I
程序员文章站
2022-07-15 23:24:15
...
方法一:
class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
double avg = 0,sum=0;
for (int i = 0; i < k; i++)
sum += nums[i];
avg = sum / k;
for (int i = k; i < nums.size(); i++)
{
sum = sum + nums[i] - nums[i - k];
avg = max(sum/k, avg);
cout<<avg<<endl;
}
return avg;
}
};
下一篇: LintCode 二叉树的层次遍历