class Solution {
public:
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
if(k<2)
return 0;
int n = nums.size();
int i=0,j=0;
int product=1,ans=0;
while(j<n)
{
product*=nums[j];
while(product>=k&&i<j)
{
product/=nums[i];
++i;
}
ans+=j-i+1; //每次扩展后会增加窗口元素个数个子数组
++j;
}
return ans;
}
};
滑动窗口模板题