leetcode 209 滑动窗口

209.png
    public int minSubArrayLen(int s, int[] nums) {
        if(s <= 0 || nums == null)
            throw new IllegalArgumentException("Illigal Arguments");

        int l = 0 , r = -1;
        int sum = 0;
        int res = nums.length + 1;

        while(l < nums.length){

            if(r + 1 < nums.length && sum < s)
                sum += nums[++r];
            else
                sum -= nums[l++];

            if(sum >= s)
                res = Math.min(res, r - l + 1);
        }
        return res == nums.length + 1 ? 0 : res;
    }

你可能感兴趣的:(leetcode 209 滑动窗口)