problem:209. 长度最小的子数组
思路:维护窗口里面的sum<=k即可
Accode:
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
long long sum =0;
int ans =1e6;
int len =nums.size();
for(int i=0,j=0;i<len;i++){
sum+=nums[i];
while(sum>=target){
if(sum>=target) ans = min(ans,i-j+1);
sum-=nums[j];
++j;
}
}
return ans==1e6?0:ans;
}
};
时间复杂度:$o(n)$