当前位置:网站首页>滑动窗口
滑动窗口
2022-06-26 10:03:00 【Code小白】
基本概念
滑动窗口是一种基于双指针的一种思想,两个指针指向的元素之间形成一个窗口。
应用场景
有关问题
1、一般给出的数据结构是数组或者字符串
2、求取某个子串或者子序列最长最短等最值问题或者求某个目标值时
3、该问题本身可以通过暴力求解
关键字
满足XXX条件(计算结果,出现次数,同时包含)
最短/最长
子串/子数组/子序列
例如:长度最小的子数组
滑动窗口思想
寻找最长
——核心:左右双指针(L,R)在起始点,R向右逐位滑动循环
——每次滑动过程中
如果:窗内元素满足条件,R向右扩大窗口,并更新最优结果
如果:窗内元素不满足条件,L向右缩小窗口
——R到达结尾
寻找最短
——核心:左右双指针(L,R)在起始点,R向右逐位滑动循环
——每次滑动过程中
如果:窗内元素满足条件,L向右缩小窗口,并更新最优结果
如果:窗内元素不满足条件,R向右扩大窗口
——R到达结尾
代码模板
最长模板
//最长模板
初始化left,right,result,bestResult
while(右指针没有到结尾){
窗口扩大,加入right对应元素,更新当前result
while(result不满足要求){
窗口缩小,移除left对应元素,left右移
}
更新最优结果bestResult
right++;
}
返回bestResult
最短模板
//最短模板
初始化left,right,result,bestResult
while(右指针没有到结尾){
窗口扩大,加入right对应元素,更新当前result
while(result满足要求){
更新最优结果bestResult
窗口缩小,移除left对应元素,left右移
}
right++;
}
返回bestResult
leetcode例题
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。
链接:https://leetcode.cn/problems/minimum-size-subarray-sum
示例 1:
输入:target = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
示例 2:
输入:target = 4, nums = [1,4,4]
输出:1
示例 3:
输入:target = 11, nums = [1,1,1,1,1,1,1,1]
输出:0
提示:
1 <= target <= 109
1 <= nums.length <= 105
1 <= nums[i] <= 105
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int left=0,right=0,minLength=0,sum=0;
while (right<nums.length){
sum=sum+nums[right];
while (sum>=target){
if (right-left+1<minLength||minLength==0){
minLength=right-left+1;
}
sum=sum-nums[left];
left++;
}
right++;
}
return minLength;
}
}
结果:
边栏推荐
- Bit operation n & (n-1), leetcode231, interview question 05.06
- consul微服务治理中心踩坑
- JS take the date of the previous month 【 pit filling 】
- AIX basic operation record
- MySQL Chapter 6 Summary
- Reasons for "unresolved external symbols" during vs or QT compilation link:
- MySQL performance monitoring and SQL statements
- MySQL 13th job - transaction management
- MySQL 9th job - connection Query & sub query
- Oracle sqlplus 查询结果显示优化
猜你喜欢

MySQL 10th job - View

Which PHP open source works deserve attention

Nuxt. JS - learning notes
![Installing MySQL under Linux [details]](/img/38/77be56c3ef3923ce4c4e5df4a96f41.png)
Installing MySQL under Linux [details]

MySQL 13th job - transaction management

Adaptiveavgpool2d does not support onnx export. Customize a class to replace adaptiveavgpool2d

Reshape a two-dimensional array with 3 rows and 3 columns to find the sum of the diagonals

Getting started with postman

QT连接MySql数据查询失败

MySQL 9th job - connection Query & sub query
随机推荐
工程数学概率论统计简明教程第二版复习大纲
哪些PHP开源作品值得关注
Developers, what is the microservice architecture?
Installing MySQL under Linux [details]
MySQL Chapter 5 Summary
MySQL 11th job - view application
代码规范 & 详细解释 husky、prettier、eslint、lint-staged 的作用和使用
Redis中执行Lua脚本
Alibaba cloud OSS - object storage service (tool)
Oracle11g reports an error when starting the database ora-27154: post/wait create failed
June training (the 26th day) - collective search
MySQL 12th job - Application of stored procedure
MySQL第十二次作业-存储过程的应用
搜索引擎高级搜索方法记录
VS或Qt编译链接过程中出现“无法解析的外部符号”的原因:
Flutter and native communication (Part 1)
Oracle sqlplus 查询结果显示优化
Moore vote, leetcode169, leetcode229
Recent work report
Using reflection to export entity data to excel