当前位置:网站首页>[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
边栏推荐
- (the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
- [Nodejs] 20. Koa2 onion ring model ----- code demonstration
- rtklib单点定位spp使用抗差估计遇到的问题及解决
- 【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
- VLSM variable length subnet mask partition tips
- [offer18] delete the node of the linked list
- (3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
- Unity3d, Alibaba cloud server, platform configuration
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- Database table splitting strategy
猜你喜欢
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
音乐播放(Toggle && PlayerPrefs)
Unity3d makes the registration login interface and realizes the scene jump
Fairygui joystick
Matlab读取GNSS 观测值o文件代码示例
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
[算法] 劍指offer2 golang 面試題2:二進制加法
Single chip Bluetooth wireless burning
FairyGUI增益BUFF數值改變的顯示
随机推荐
Derivation of logistic regression theory
MySQL shutdown is slow
isEmpty 和 isBlank 的用法区别
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
GPS高程拟合抗差中误差的求取代码实现
NRF24L01 troubleshooting
Fairygui loop list
[leetcode19]删除链表中倒数第n个结点
[899] ordered queue
NovAtel 板卡OEM617D配置步骤记录
[Offer18]删除链表的节点
Halcon knowledge: gray_ Tophat transform and bottom cap transform
341. Flatten nested list iterator
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
Compile GDAL source code with nmake (win10, vs2022)
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
最短Hamilton路径 (状压DP)
Lock wait timeout exceeded try restarting transaction
基于rtklib源码进行片上移植的思路分享