目录
一、题目
二、代码
一、题目
643. 子数组最大平均数 I - 力扣(LeetCode)
二、代码
class Solution {
public:double findMaxAverage(vector<int>& nums, int k) {double Average = INT_MIN;double sum = nums[0];int left = 0, right = 0;while (right < nums.size()){if (right - left + 1 == k)//满足长度为k{if (sum / (right - left + 1) > Average){Average = sum / (right - left + 1);sum -= nums[left++];}else{if (right + 1 == nums.size()){break;}sum += nums[++right];}}else if (right - left + 1 < k){if (right + 1 == nums.size()){break;}sum += nums[++right];}else//长度大于k,左指针右移{sum -= nums[left++];}}return Average;}
};