当前位置:网站首页>[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
题目1:

思路1: 滑动窗口
代码
func numSubarrayProductLessThanK(nums []int, k int) int {
//start: 10:28,... end 11.23
//思路1: 滑动窗口
//参数处理
if len(nums) == 0 || k <= 0 {
return 0
}
//滑动窗口
res := 0
left,right,product := 0,0,1
for ;right < len(nums);right++ {
product *= nums[right]
for left <= right && product >= k {
product /= nums[left]
left ++
}
if left <= right {
res += right-left +1
}
}
return res
}
测试
边栏推荐
猜你喜欢

Naive Bayesian theory derivation

Unity3d makes the registration login interface and realizes the scene jump

ESP8266连接onenet(旧版MQTT方式)

Conditional probability

Fabrication of fairygui simple Backpack

FairyGUI摇杆

Fairygui joystick

(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis

Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY

PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
随机推荐
Special palindromes of daily practice of Blue Bridge Cup
Theoretical derivation of support vector machine
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
2021.11.10汇编考试
基本Dos命令
FairyGUI增益BUFF数值改变的显示
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
What is the maximum length of MySQL varchar field
@The difference between Autowired and @resource
FairyGUI摇杆
音乐播放(Toggle && PlayerPrefs)
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
Database course design: college educational administration management system (including code)
Fairygui loop list
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
Agile development helps me
NRF24L01 troubleshooting
[Chongqing Guangdong education] Shandong University College Physics reference materials
第一人称视角的角色移动
【GNSS】抗差估计(稳健估计)原理及程序实现