Rust 力扣 - 643. 子数组最大平均数 I-题解代码

impl Solution {
    pub fn find_max_average(nums: Vec<i32>, k: i32) -> f64 {
        let mut sum = 0;

        // 填满长度为k的窗口
        for i in 0..k as usize {
            sum += nums[i];
        }

        let mut max_sum = sum;

        // 移动窗口,并记录最大的窗口和
        for i in k as usize..nums.len() {
            sum += nums[i] - nums[i - k as usize];
            max_sum = max_sum.max(sum);
        }

        max_sum as f64 / k as f64
    }
}
上一篇:Java基础(8)异常


下一篇:update-alternatives(选择工具)-1 update-alternatives介绍