长度最小的子数组 (Minimum Size Subarray Sum)

 

思路:滑动窗口的思想,先移动右指针,然后慢慢缩减左指针

// @Title: 长度最小的子数组 (Minimum Size Subarray Sum)
// @Author: qisiii
// @Date: 2024-09-11 16:41:46
// @Runtime: 1 ms
// @Memory: 56.7 MB
// @comment: 滑动窗口的思想,先移动右指针,然后慢慢缩减左指针
// @flag: GREEN
class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int start = 0, end = 0;
        int sum = 0, len = nums.length;
        boolean flag = false;
        while (end < nums.length) {
            sum = sum + nums[end];
            while (sum >= target) {
                len = Math.min(len, end - start + 1);
                flag = true;
                sum -= nums[start];
                start++;
            }
            end++;
        }
        return flag ? len : 0;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18