当前位置:网站首页>leetcode/乘积小于K 的连续子数组的个数
leetcode/乘积小于K 的连续子数组的个数
2022-07-29 01:08:00 【xcrj】
代码
package com.xcrj;
/** * 剑指 Offer II 009. 乘积小于 K 的子数组 * 给定一个正整数数组 nums和整数 k ,请找出该数组内乘积小于 k 的连续的子数组的个数。 * (数组元素乘积小于k&&数组元素连续)有多少个这样的数组 */
public class Solution9 {
/** * 滑动窗口 * !!!使用不同的指针达到不同的目的 * i j开始都指向0 * j后移直到product>=s * i后移直到product<s * sum+=j-i+1 */
public int numSubarrayProductLessThanK(int[] nums, int k) {
int i = 0;
int j = 0;
int product = 1;
int sum = 0;
while (j < nums.length) {
product *= nums[j];
// 输入[1,2,3] 0时,product一直为0,0/任何数=0
// i<j避免i一直加,i==j子数组长度为1满足<k也可以
while (i <= j && product >= k) {
product /= nums[i];
i++;
}
sum += j - i + 1;
j++;
}
return sum;
}
public static void main(String[] args) {
Solution9 solution9 = new Solution9();
System.out.println(solution9.numSubarrayProductLessThanK(new int[]{
10, 5, 2, 6}, 100));
}
}
参考
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/ZVAVXX/solution/cheng-ji-xiao-yu-k-de-zi-shu-zu-by-leetc-xqx8/
来源:力扣(LeetCode)
边栏推荐
- Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!
- How to choose professional, safe and high-performance remote control software
- 关于df[‘某一列名’][序号]
- Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
- ELS new box falls
- 正则过滤数据学习笔记(①)
- [7.21-26] code source - [sports festival] [Dan fishing war] [maximum weight division]
- Some summaries of ibatis script and provider
- 承办首届算力大会,济南胜在何处?
- 【Golang】- runtime.Goexit()
猜你喜欢

Overview of Qualcomm 5g intelligent platform

Planning mathematics final exam simulation II

Why does stonedb dare to call it the only open source MySQL native HTAP database in the industry?
![[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's "flywheel effect"
![[WesternCTF2018]shrine](/img/c1/c099f8930902197590052630281258.png)
[WesternCTF2018]shrine

How many of the top ten test tools in 2022 do you master

Six noteworthy cloud security trends in 2022

The brutal rule of blackmail software continues, and attacks increase by 105%

Reinforcement learning (II): SARS, with code rewriting

【流放之路-第三章】
随机推荐
ELS stop at all
golang启动报错【已解决】
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
【7.21-26】代码源 - 【好序列】【社交圈】【namonamo】
Some summaries of ibatis script and provider
Wonderful use of data analysis
5g commercial third year: driverless "going up the mountain" and "going to the sea"
[7.27] code source - [deletion], [bracket sequence], [number replacement], [game], [painting]
Merkel Studio - harmonyos implementation list to do
ELS new box falls
规划数学期末考试模拟二
Explanation of yocto project directory structure
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new engine for the digitalization of all elements of traffic in Shenzhen
Stonedb invites you to participate in the open source community monthly meeting!
【Golang】- runtime.Goexit()
Practical experience of Google cloud spanner
知道创宇上榜CCSIP 2022全景图多个领域
Use of packet capturing tool Charles
[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area
Network security litigation risk: four issues that chief information security officers are most concerned about