Skip to main content

🟡 剑指 Offer II 009. 乘积小于 K 的子数组

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

题解1#

滑动窗口。偷瞄了官方题解

class Solution {    public int numSubarrayProductLessThanK(int[] nums, int k) {        int left = 0, right = 0;        int res = 1;        int ans = 0;        while (right<nums.length) {            res *= nums[right];            while (res >= k && left <= right) {                res /= nums[left];                left += 1;            }            if (left <= right) {                ans += right-left+1;            }            right += 1;        }        return ans;    }}