当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
2022-07-06 12:51:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 8: And greater than or equal to k The shortest subarray of
subject 1:
Ideas 1: The sliding window
// Ideas : The sliding window
// The pointer [left,right] Record the interval of continuous subarray , And record the interval and sum
//if sum < target; right ++ ;sum += nums[right]
//if sum >= target; Update minimum length minLen; sum -= nums[left]; left++
Code
func minSubArrayLen(target int, nums []int) int {
// Ideas : The sliding window
// The pointer [left,right] Record the interval of continuous subarray , And record the interval and sum
//if sum < target; right ++ ;sum += nums[right]
//if sum >= target; Update minimum length minLen; sum -= nums[left]; left++
// Processing parameters
if len(nums) ==0 || target <= 0{
return -1
}
// The sliding window
minLen := 0
left,right,sum := 0,0,nums[0]
for left <= right{
if sum < target {
right ++
// There will be pointer overflow here todo
if right == len(nums) {
return minLen
}
sum += nums[right]
}else {
// Update minimum length
tempLen := right - left + 1
if tempLen < minLen || minLen == 0{
minLen = tempLen
}
sum -= nums[left]
left ++
}
}
return minLen
}
test
边栏推荐
- Special palindromes of daily practice of Blue Bridge Cup
- 3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
- KF UD分解之UD分解基础篇【1】
- 第一人称视角的角色移动
- Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
- wsl常用命令
- 使用rtknavi进行RT-PPP测试
- Single chip Bluetooth wireless burning
- GPS高程拟合抗差中误差的求取代码实现
- Knowledge system of digital IT practitioners | software development methods -- agile
猜你喜欢
2021.11.10汇编考试
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Latex learning
Matlab读取GNSS 观测值o文件代码示例
Unity3d makes the registration login interface and realizes the scene jump
Fabrication d'un sac à dos simple fairygui
2021.11.10 compilation examination
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
FGUI工程打包发布&导入Unity&将UI显示出来的方式
NovAtel 板卡OEM617D配置步骤记录
随机推荐
Programming homework: educational administration management system (C language)
C programming exercise
Matlab读取GNSS 观测值o文件代码示例
Idea problem record
There is no red exclamation mark after SVN update
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
基于rtklib源码进行片上移植的思路分享
FairyGUI简单背包的制作
Special palindromes of daily practice of Blue Bridge Cup
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
idea中导包方法
Lean product development - Lean Software Development & lean product development
[算法] 剑指offer2 golang 面试题1:整数除法
SSD technical features
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
Agile development helps me
Introduction to the daily practice column of the Blue Bridge Cup
FairyGUI条子家族(滚动条,滑动条,进度条)