当前位置:网站首页>209. Subarray with the smallest length
209. Subarray with the smallest length
2022-07-26 08:59:00 【apprentice.】
subject :
Answer key :
For this question , There is a word in the question that requires Continuous subarray , For this requirement continuous , Sliding windows should be considered first . Then the question is to find the shortest continuous sub array, so when traversing this array , The general idea is as follows :
- Combination of values in the window < target : The length of the window +1
- Combination of values in the window >= target : Compare the current window length with the shortest length recorded , And the window length -1
For window length +1 operation , Is to expand an element on the right side of the window , For window length -1 The operation of , Is to subtract an element from the left
Here are Go The language code :
func minSubArrayLen(target int, nums []int) int {
min := len(nums) + 1
winLen := 1
sum := nums[0]
for i := 0; ; {
if sum < target {
if i+winLen == len(nums) {
break
}
winLen++
sum = sum + nums[i+winLen-1]
}
if sum >= target {
if winLen < min {
min = winLen
}
sum = sum - nums[i]
i++
winLen--
}
}
if min == len(nums)+1 {
return 0
} else {
return min
}
}
边栏推荐
- IC's first global hacking bonus is up to US $6million, helping developers venture into web 3!
- Which financial product has the highest yield in 2022?
- node-v下载与应用、ES6模块导入与导出
- QtCreator报错:You need to set an executable in the custom run configuration.
- Node-v download and application, ES6 module import and export
- Pop up window in Win 11 opens with a new tab ---firefox
- js闭包:函数和其词法环境的绑定
- ES6 modular import and export) (realize page nesting)
- NPM add source and switch source
- [encryption weekly] has the encryption market recovered? The cold winter still hasn't thawed out. Take stock of the major events that occurred in the encryption market last week
猜你喜欢
随机推荐
220. Presence of repeating element III
The effective condition of MySQL joint index and the invalid condition of index
Cadence(十)走线技巧与注意事项
Kotlin properties and fields
数据库操作 技能6
Pan micro e-cology8 foreground SQL injection POC
(1) CTS tradefed test framework environment construction
[recommended collection] MySQL 30000 word essence summary index (II) [easy to understand]
Arbitrum Nova release! Create a low-cost and high-speed dedicated chain in the game social field
Review notes of Microcomputer Principles -- zoufengxing
unity简易消息机制
C # use npoi to operate Excel
数据库操作 题目一
PHP page value transfer
ES6 modular import and export) (realize page nesting)
After MySQL 8 OCP (1z0-908), hand in your homework
ES6模块化导入导出)(实现页面嵌套)
[search topics] flood coverage of search questions after reading the inevitable meeting
深度学习常用激活函数总结
The largest number of statistical absolute values --- assembly language








