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

边栏推荐
- GPS高程拟合抗差中误差的求取代码实现
- [算法] 劍指offer2 golang 面試題2:二進制加法
- It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
- Halcon knowledge: gray_ Tophat transform and bottom cap transform
- 堆排序【手写小根堆】
- Expected value (EV)
- Derivation of logistic regression theory
- Special palindromes of daily practice of Blue Bridge Cup
- Lean product development - Lean Software Development & lean product development
- Unity场景跳转及退出
猜你喜欢

Idea problem record

Fabrication d'un sac à dos simple fairygui
![[Nodejs] 20. Koa2 onion ring model ----- code demonstration](/img/a8/a4390238685903b63bb036206f8dcb.jpg)
[Nodejs] 20. Koa2 onion ring model ----- code demonstration

What are the advantages of using SQL in Excel VBA

【无标题】

Halcon knowledge: gray_ Tophat transform and bottom cap transform

FairyGUI簡單背包的制作

NovAtel 板卡OEM617D配置步骤记录

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

Latex learning
随机推荐
KF UD分解之伪代码实现进阶篇【2】
Compile GDAL source code with nmake (win10, vs2022)
Guided package method in idea
[leetcode622] design circular queue
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
NovAtel 板卡OEM617D配置步骤记录
Fairygui gain buff value change display
微信小程序开发心得
VLSM variable length subnet mask partition tips
使用rtknavi进行RT-PPP测试
What are the functions and features of helm or terrain
Database table splitting strategy
dosbox第一次使用
How to reduce the shutdown time of InnoDB database?
[Leetcode15]三数之和
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
Teach you to release a DeNO module hand in hand
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
Excel导入,导出功能实现