0209. Minimum Size Subarray Sum

209. Minimum Size Subarray Sum #

题目 #

给定一个含有n个正整数的数组和一个正整数target。找出该数组中满足其和>=target的长度最小的连续子数组,并返回其长度。如果不存在符合条件的子数组,返回0

思路 #

代码 #

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        if (nums == null) return 0;
        int left=0, right=0, len=0, cumSum=0;
        while (left <= right && right < nums.length) {
            curSum += nums[right];
            if (curSum >= target) {
                if (len == 0 || len > right-left+1) len = right-left+1;
                curSum -= nums[left];
                left += 1;
                curSum -= nums[right];
            }
            else right += 1;
        }
        return len;
    }
}