核心思路:计算low,fast区间的值,如果小于target就移动fast指针,如果大于等于就移动low指针,并记录下fast - low即区间长度!
其实这里滑动窗口只是 双指针的应用(快慢指针)!
public int minSubArrayLen(int target, int[] nums) {
int res = Integer.MAX_VALUE ;
int sum = 0;
int i =0 ;
int subLength =0;
for (int j = 0 ;j < nums.length ; j++) {
sum += nums[j];
while (sum >= target) {
subLength = (j - i + 1);
res = res < subLength ? res : subLength;
sum -= nums[i++];
}
}
return res == Integer.MAX_VALUE ? 0 : res;
}