滑动窗口模板
func function(nums []int) {
length := len(nums)
left, right := 0, 0
for right < length{
// 0.right右滑变量的改动
right++
// 1.left右滑的条件
for left右滑的条件{
// 2.判断区间是否满足条件
// 3.left右滑变量的改动
left++
}
}
}
思考:
- 为什么模板中的内层for有时可以优化为if
- 为什么有时可以不维护res的值,而直接返回len-L或者R-L
- 当题目要求的不是“符合条件的窗口的最大值”,而是“符合条件的窗口的最小值”,模板如何修改
| Topic |
|---|
| 209. 长度最小的子数组 |
| 3. 无重复字符的最长子串 |
| 76. 最小覆盖子串 |
| 1208. 尽可能使字符串相等 |
| 424. 替换后的最长重复字符 |
| 485. 最大连续 1 的个数 |
ref:
https://blog.csdn.net/m0_46202073/article/details/113636767
https://blog.csdn.net/m0_46202073/article/details/113666333