class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int left = 0, right = 0;
int sum = 0;
int len = 0;
int min_len = INT_MAX;
while(right < nums.size()){
while(right < nums.size() && left <= right){
if(sum >= target){
min_len = min(min_len, len);
break;
}
sum += nums[right];
len++;
right++;
}
while(sum >= target){ //当总和减去left的数后仍然大于target,则继续将left的值减去。
sum -= nums[left];
len--;
left++;
min_len = min(min_len, len+1);
}
}
if(min_len == INT_MAX)
return 0;
else
return min_len;
}
};
07-20
787
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-12
1120
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)