209. 长度最小的子数组 - 力扣(LeetCode)
题目描述
解题思路
滑动窗口
class Solution {
public int minSubArrayLen(int target, int[] nums) {
// 滑动窗口
int n = nums.length;
int left = 0, right = 0;
int sum = 0;
int len = Integer.MAX_VALUE;
while(right < n){
sum += nums[right];
right ++;
while(sum >= target){
if(right - left < len){
len = right - left;
}
sum -= nums[left];
left ++;
}
}
return len == Integer.MAX_VALUE ? 0 : len;
}
}