LeetCode713.乘积小于K的子数组

题目来源:

https://leetcode-cn.com/problems/subarray-product-less-than-k/

题目描述:

LeetCode713.乘积小于K的子数组_第1张图片

 代码如下:

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

 

你可能感兴趣的:(LeetCode)