当前位置:网站首页>[算法] 剑指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
}
测试
边栏推荐
- [leetcode19] delete the penultimate node in the linked list
- 【RTKLIB 2.4.3 b34 】版本更新简介一
- Halcon knowledge: gray_ Tophat transform and bottom cap transform
- Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
- 数据库课程设计:高校教务管理系统(含代码)
- Detailed explanation of truncate usage
- @The difference between Autowired and @resource
- MySQL error warning: a long semaphore wait
- [offer78] merge multiple ordered linked lists
- [Offer18]删除链表的节点
猜你喜欢
NovAtel 板卡OEM617D配置步骤记录
Fabrication of fairygui simple Backpack
FairyGUI簡單背包的制作
FairyGUI简单背包的制作
单片机蓝牙无线烧录
程序设计大作业:教务管理系统(C语言)
What are the advantages of using SQL in Excel VBA
Unity3D制作注册登录界面,并实现场景跳转
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
rtklib单点定位spp使用抗差估计遇到的问题及解决
随机推荐
FairyGUI人物状态弹窗
燕山大学校园网自动登录问题解决方案
[offer29] sorted circular linked list
Theoretical derivation of support vector machine
JS function promotion and declaration promotion of VaR variable
Flink late data processing (3)
Guided package method in idea
@The difference between Autowired and @resource
單片機藍牙無線燒錄
[899] ordered queue
Fabrication of fairygui simple Backpack
What are the advantages of using SQL in Excel VBA
First use of dosbox
FairyGUI按钮动效的混用
Expected value (EV)
[offer9]用两个栈实现队列
About using @controller in gateway
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
Halcon knowledge: gray_ Tophat transform and bottom cap transform