当前位置:网站首页>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)
边栏推荐
- 【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
- 【7.21-26】代码源 - 【好序列】【社交圈】【namonamo】
- Come on, handwritten RPC S2 serialization exploration
- 【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
- As long as I run fast enough, it won't catch me. How does a high school student achieve a 70% salary increase under the epidemic?
- 移动通信——基于卷积码的差错控制系统仿真模型
- [the road of Exile - Chapter 5]
- Know that Chuangyu is listed in many fields of ccsip 2022 panorama
- Stonedb invites you to participate in the open source community monthly meeting!
- 分布式开发漫谈
猜你喜欢

JVM learning minutes
![[hcip] experiment of republishing and routing strategy](/img/26/d62d3083796757d33c0a513f842176.png)
[hcip] experiment of republishing and routing strategy
![[golang] use select {}](/img/30/fa593ec682a40c47689c1fd88f9b83.png)
[golang] use select {}

【观察】三年跃居纯公有云SaaS第一,用友YonSuite的“飞轮效应”

数学建模——派出所选址
![[golang] network connection net.dial](/img/8d/7ef64cb63cbd230e5ac1655b86786f.png)
[golang] network connection net.dial

What is the ISO assessment? How to do the waiting insurance scheme
![[netding cup 2020 rosefinch group]nmap](/img/22/1fdf716a216ae26b9110b2e5f211f6.png)
[netding cup 2020 rosefinch group]nmap

TDA75610-I2C-模拟功放I2C地址的确定

【流放之路-第七章】
随机推荐
[search] - iteration deepening / bidirectional dfs/ida*
[7.21-26] code source - [square count] [dictionary order minimum] [Z-type matrix]
[the road of Exile - Chapter 8]
Stonedb invites you to participate in the open source community monthly meeting!
LeetCode 113:路径总和 II
Know that Chuangyu is listed in many fields of ccsip 2022 panorama
Day01作业
【流放之路-第三章】
ASCII code table
[7.27] code source - [deletion], [bracket sequence], [number replacement], [game], [painting]
golang启动报错【已解决】
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
Slow storage scheme
【流放之路-第八章】
[the road of Exile - Chapter 7]
The brutal rule of blackmail software continues, and attacks increase by 105%
DSP vibration seat
【流放之路-第七章】
We summarized the three recommendations for the use of Nacos and first published the Nacos 3.0 plan for the 4th anniversary of the open source of Nacos
[understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.