leetcode-643. 子数组最大平均数 I
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
}
本文来自博客园,作者:吴丹阳-V,转载请注明原文链接:https://www.cnblogs.com/wudanyang/p/17023697.html