当前位置:网站首页>[算法] 剑指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
}
测试

边栏推荐
- [offer78]合并多个有序链表
- rtklib单点定位spp使用抗差估计遇到的问题及解决
- 【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
- 2021.11.10汇编考试
- 抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
- 【GNSS】抗差估计(稳健估计)原理及程序实现
- Halcon knowledge: gray_ Tophat transform and bottom cap transform
- GNSS定位精度指标计算
- Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
- (四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
猜你喜欢

Fabrication d'un sac à dos simple fairygui

FairyGUI人物状态弹窗

Office prompts that your license is not genuine pop-up box solution

Fairygui joystick

ORA-02030: can only select from fixed tables/views
![[Chongqing Guangdong education] Shandong University College Physics reference materials](/img/56/4ac44729c3e480a4f779d6a890363a.jpg)
[Chongqing Guangdong education] Shandong University College Physics reference materials

SVN更新后不出现红色感叹号

FairyGUI循環列錶

Prove the time complexity of heap sorting

地球围绕太阳转
随机推荐
Fairygui loop list
Special palindromes of daily practice of Blue Bridge Cup
KF UD分解之UD分解基础篇【1】
Meanings and differences of PV, UV, IP, VV, CV
微信小程序开发心得
Unity3d, Alibaba cloud server, platform configuration
ORA-02030: can only select from fixed tables/views
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
dosbox第一次使用
Office prompts that your license is not genuine pop-up box solution
[leetcode15] sum of three numbers
如何给Arduino项目添加音乐播放功能
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
Acwing-116 pilot brother
FairyGUI复选框与进度条的组合使用
第一人称视角的角色移动
[offer78]合并多个有序链表
Unity3D,阿里云服务器,平台配置
[Offer18]删除链表的节点
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)