滑动窗口的妙用!!
int minSubArrayLen(int target, int* nums, int numsSize) {
int sum=nums[0];//区间head到tail的和
int head=0,tail=0;
int minn=numsSize;
int tag=0;
if(numsSize==0) return -1;
while(head<=tail && tail<numsSize){
if(sum<target ){
tail++;
if(tail==numsSize) break;
sum+=nums[tail];
}else if(sum>=target ){
tag=1;
if(minn>(tail-head+1)) minn=tail-head+1;
sum-=nums[head++];
}
}
if(tag==0){
return 0;
}
return minn;
}
结果: