题解 | 乘积小于K的子数组数量
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param nums int整型一维数组
# @param k int整型
# @return int整型
#
class Solution:
def subarrayCnt(self , nums: List[int], k: int) -> int:
# write code here
if k<=1:
return 0
n=len(nums)
s=1
left=0
ans=0
for right in range(n):
s*=nums[right]
while s>=k:
s/=nums[left]
left+=1
ans+=right-left+1
return ans

查看18道真题和解析