LeetCode 209. 长度最小的子数组 (滑动窗口)

LeetCode 209. 长度最小的子数组

class Solution {
     
    public int minSubArrayLen(int s, int[] nums) {
     
        int start = 0;
        int sum = 0;
        int minLen = nums.length + 1;
        int mark = 0;
        for (int end = 0; end < nums.length; end++) {
     
            sum += nums[end];
            while (sum >= s) {
     
                sum-=nums[start];
                minLen = Math.min(minLen, end - start + 1);
                start++;
                mark = 1;
            }
        }
        return nums.length > 0 && mark == 1 ? minLen : 0;
    }
}

你可能感兴趣的:(#,滑动窗口)