**643. 子数组最大平均数 I(前缀和加滑动窗口)**

Untitled

public double findMaxAverage(int[] nums, int k) {
    int n = nums.length;
    int[] prefix = new int[n + 1];
    int ans = Integer.MIN_VALUE;
    for(int i = 1; i <= n; i++) {
        prefix[i] = prefix[i - 1] + nums[i - 1];
    } 
    for(int i = 0; i + k <= n; i++) {
        ans = Math.max(ans, prefix[i + k] - prefix[i]);
    }
    return 1.0 * ans / k;
}

**456. 定长子串中元音的最大数目(前缀和加滑动窗口)**

Untitled

public int maxVowels(String s, int k) {
    int n = s.length();
    int[] prefix = new int[n + 1];
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        prefix[i] = prefix[i - 1] + isVowel(s.charAt(i - 1));
    }
    for(int i = 0; i + k <= n; i++) {
        ans = Math.max(ans, prefix[i + k] - prefix[i]);
    }
    return ans;
}
private int isVowel(char c) {
    char[] s = {'a','e','i','o','u'};
    for(char sc: s) {
        if(sc == c) return 1;
    }
    return 0;
}

**1004. 最大连续1的个数 III(窗口大小不确定)**

Untitled

public int longestOnes(int[] nums, int k) {
    int ans = 0;
    int l = 0, r = 0;
    int n = nums.length;
    int sum = 0;
    int help = k;
    while(r < n) {
        // 右端点到最远距离
        while(r < n && (nums[r] != 0 || help > 0)) {
            if(nums[r] == 0) help--;
            sum++; r++;
        }
        ans = Math.max(ans, sum);
        // 收缩左端点
        while(l < r && nums[l] != 0) {
            l++;
            sum--;
        }
        l++;
        sum--;
        help++;
    }
    return ans;
}

1493. 删掉一个元素以后全为 1 的最长子数组

Untitled

public int longestSubarray(int[] nums) {
    int ans = 0, sum = 0;
    int n = nums.length;
    int l = 0, r = 0;
    while(r < n) {
        while(r < n && nums[r] == 1) {sum++; r++;}
        // 特殊情况处理
        if(r == n) sum--;
        r++;
        while(r < n && nums[r] == 1) {sum++; r++;}
        ans = Math.max(ans, sum);
        while(l < r && l < n && nums[l] == 1) {sum--; l++;}
        l++;
    }
    return ans;
}