当前位置:网站首页>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)
边栏推荐
- Basic label in body
- 【Golang】- runtime.Goexit()
- 为什么 BI 软件都搞不定关联分析
- [the road of Exile - Chapter 4]
- 九天后我们一起,聚焦音视频、探秘技术新发展
- Internship: tool class writing for type judgment
- How to deal with the DDoS attack on the game server and how to defend it?
- Lxml web page capture the most complete strategy
- DSP vibration seat
- Tda75610-i2c-determination of I2C address of analog power amplifier
猜你喜欢

The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities

Where will Jinan win in hosting the first computing power conference?

Analyzing the function of human-computer interface module of runtime manager based on autoware
![[hcip] experiment of republishing and routing strategy](/img/26/d62d3083796757d33c0a513f842176.png)
[hcip] experiment of republishing and routing strategy

【流放之路-第六章】

DSP震动座椅
![[web technology] 1395 esbuild bundler HMR](/img/74/be75c8f745f18b374ed15c8e1b4466.png)
[web technology] 1395 esbuild bundler HMR

【流放之路-第三章】

ciscn 2022 华中赛区 misc
![[10:00 public class]: application exploration of Kwai gpu/fpga/asic heterogeneous platform](/img/e7/1d06eba0e50eeb91d2d5da7524f4af.png)
[10:00 public class]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
随机推荐
[search] - DFS pruning and optimization
Lxml web page capture the most complete strategy
【7.27】代码源 - 【删数】【括号序列】【数字替换】【游戏】【画画】
【流放之路-第六章】
[10:00 public class]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
560 and K
Lua log implementation -- print table
OpenGL development with QT (II) drawing cube
[understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
Leetcode 112: path sum
Reinforcement learning (II): SARS, with code rewriting
The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
golang启动报错【已解决】
Network security litigation risk: four issues that chief information security officers are most concerned about
Internship: tool class writing for type judgment
规划数学期末考试模拟二
How many of the top ten test tools in 2022 do you master
Process -- user address space and kernel address space
[网鼎杯 2020 朱雀组]Nmap
秘术冬潮烙技能搭配