枚举子数组问题,常见有固定一个点,枚举另一个端点,还有枚举中间点。
本题使用双指针算法,对右端点进行枚举,每次累加[l, r]区间内,所有以右端点为结尾的子数组对答案的贡献度,也就是长度 r- l + 1
class Solution: def countSubarrays(self, nums: List[int], k: int) -> int: res = left = s = 0 for right, val in enumerate(nums): s += val while s * (right - left + 1) >= k: s -= nums[left] left += 1 res += right - left + 1 return res
标签:right,nums,int,res,2302,数组,指针,left From: https://www.cnblogs.com/zk6696/p/17872630.html