643. 子数组最大平均数 I - 力扣(Leetcode)
滑动窗口,判断好边界条件即可
func findMaxAverage(nums []int, k int) float64 {
begin, end := 0, k-1
if end >= len(nums) {
end = len(nums) - 1
}
curSum := 0
for i := begin; i <= end; i++ {
curSum += nums[i]
}
maxAvg := float64(curSum) / float64(end - begin + 1)
end++
for ; end < len(nums); end++ {
// fmt.Println(begin, end)
curSum += nums[end]
curSum -= nums[begin]
avg := float64(curSum) / float64(end - begin)
if avg > maxAvg {
maxAvg = avg
}
begin++
}
return maxAvg
}
标签:maxAvg,begin,end,nums,平均数,643,leetcode
From: https://www.cnblogs.com/wudanyang/p/17023697.html