当前位置:网站首页>[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

边栏推荐
- Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
- What are the advantages of using SQL in Excel VBA
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
- idea中好用的快捷键
- 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
- [算法] 剑指offer2 golang 面试题10:和为k的子数组
- Fairygui loop list
- [offer9] implement queues with two stacks
- 雇佣收银员【差分约束】
猜你喜欢

PR 2021 quick start tutorial, first understanding the Premiere Pro working interface

Unity3d makes the registration login interface and realizes the scene jump
![[算法] 剑指offer2 golang 面试题1:整数除法](/img/e6/f17135207b3540ec58e5a9eed54220.png)
[算法] 剑指offer2 golang 面试题1:整数除法
![[Nodejs] 20. Koa2 onion ring model ----- code demonstration](/img/a8/a4390238685903b63bb036206f8dcb.jpg)
[Nodejs] 20. Koa2 onion ring model ----- code demonstration

抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现

Easy to use shortcut keys in idea

【无标题】

RTKLIB: demo5 b34f.1 vs b33

FairyGUI摇杆
![[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等](/img/11/ee0628a68542236fc641966579a31a.png)
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
随机推荐
[899]有序队列
程序设计大作业:教务管理系统(C语言)
InnoDB dirty page refresh mechanism checkpoint in MySQL
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
[Leetcode15]三数之和
How to add music playback function to Arduino project
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
服务未正常关闭导致端口被占用
dosbox第一次使用
Theoretical derivation of support vector machine
(core focus of software engineering review) Chapter V detailed design exercises
[算法] 劍指offer2 golang 面試題2:二進制加法
最短Hamilton路径 (状压DP)
Latex learning
Prove the time complexity of heap sorting
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
Teach you to release a DeNO module hand in hand
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
rtklib单点定位spp使用抗差估计遇到的问题及解决