题:https://leetcode.com/problems/subarray-product-less-than-k/
思路
这个的 滑动窗口 很有意思,
while 循环中 不是为了 获得 结果,而是 对 窗口 进行修正。
因为 本身 窗口 是 答案的解。
这里 有 看见 了 一种 新思路。
从 统计答案的角度来说,是从 窗口 的右端来统计。
以窗口 左端为 子串尾部 的 数量。
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
if(k<1)
return 0;
int tsum = 1;
int start = 0;
int res = 0;
for(int i = 0 ; i < nums.length ;i ++){
tsum *= nums[i];
while(tsum>=k && start <= i){
tsum = tsum/nums[start++];
}
res += i - start +1;
}
return res;
}
}