当前位置:网站首页>[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
题目1:

思路1: 滑动窗口
//思路: 滑动窗口
//指针 [left,right] 记录连续子数组的区间,并记录区间和 sum
//if sum < target; right ++ ;sum += nums[right]
//if sum >= target; 更新最小长度 minLen; sum -= nums[left]; left++
代码
func minSubArrayLen(target int, nums []int) int {
//思路: 滑动窗口
//指针 [left,right] 记录连续子数组的区间,并记录区间和 sum
//if sum < target; right ++ ;sum += nums[right]
//if sum >= target; 更新最小长度 minLen; sum -= nums[left]; left++
//参数处理
if len(nums) ==0 || target <= 0{
return -1
}
//滑动窗口
minLen := 0
left,right,sum := 0,0,nums[0]
for left <= right{
if sum < target {
right ++
//这里会有指针溢出 todo
if right == len(nums) {
return minLen
}
sum += nums[right]
}else {
//更新最小长度
tempLen := right - left + 1
if tempLen < minLen || minLen == 0{
minLen = tempLen
}
sum -= nums[left]
left ++
}
}
return minLen
}
测试

边栏推荐
- Office prompts that your license is not genuine pop-up box solution
- By v$rman_ backup_ job_ Oracle "bug" caused by details
- Containers and Devops: container based Devops delivery pipeline
- Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
- KF UD分解之UD分解基础篇【1】
- [leetcode19] delete the penultimate node in the linked list
- [Nodejs] 20. Koa2 onion ring model ----- code demonstration
- Detailed explanation of truncate usage
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
- Prove the time complexity of heap sorting
猜你喜欢

(5) Introduction to R language bioinformatics -- ORF and sequence analysis

Derivation of logistic regression theory

Gravure sans fil Bluetooth sur micro - ordinateur à puce unique

Unity场景跳转及退出

Teach you to release a DeNO module hand in hand

Unity3d makes the registration login interface and realizes the scene jump

Esp8266 connect onenet (old mqtt mode)

ESP8266连接onenet(旧版MQTT方式)

Naive Bayesian theory derivation

Conditional probability
随机推荐
MySQL shutdown is slow
RTKLIB: demo5 b34f.1 vs b33
Derivation of logistic regression theory
KF UD分解之UD分解基础篇【1】
[Chongqing Guangdong education] Shandong University College Physics reference materials
Combination of fairygui check box and progress bar
JS function promotion and declaration promotion of VaR variable
rtklib单点定位spp使用抗差估计遇到的问题及解决
[leetcode622]设计循环队列
341. Flatten nested list iterator
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
Mysql database index
FairyGUI人物状态弹窗
[Offer29] 排序的循环链表
Unity3D,阿里云服务器,平台配置
Liste des boucles de l'interface graphique de défaillance
Guided package method in idea
C programming exercise
How to improve the deletion speed of sequential class containers?
Talking about the startup of Oracle Database