【LeetCode】713. 乘积小于K的子数组
作者:互联网
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;
}
};
滑动窗口模板题
标签:乘积,nums,int,713,Solution,&&,LeetCode 来源: https://www.cnblogs.com/zzxcm/p/15864619.html