赞
踩
也叫尺取法(可能也不一定相等,大概就是这样),可以用来解决一些查找满足一定条件的连续区间的性质(长度等)的问题。由于区间连续,因此当区间发生变化时,可以通过旧有的计算结果对搜索空间进行剪枝,这样便减少了重复计算,降低了时间复杂度。往往类似于“请找到满足xx的最x的区间(子串、子数组)的xx”这类问题都可以使用该方法进行解决。
核心思路
形成窗口,窗口实际上是两个指针之间形成的区域,关键点就是这两个指针如何进行移动(一般经验是,右指针无脑移动,左指针根据条件移动)
典型例题
题目:
找到改数组中满足和 “大于等于”target的长度最小的,连续子数组。
分析:
采用滑动窗口,左指针不动,右指针不断滑动,计算sum,当sum>=target时,计算当前窗口的长度;
然后右指针不动,左指针滑动,一直到sum<target时,跳出内层循环,进行下一次循环
然后还是先移动右指针,在移动左指针...
代码
public static int minSubArrayLen1(int target, int[] nums) { // 定义左右指针 int left = 0; int rigth = 0; // 定义一个sum变量,保存滑动窗口中的所有和 int sum = 0; // 定义变量保存窗口最小的宽度,初始值设置为int类型最大值 int res = Integer.MAX_VALUE; // 外层循环,右指针移动 while (rigth < nums.length) { sum += nums[rigth]; // 内层循环,左指针移动 while (sum >= target) { res = Math.min(res, rigth - left + 1); sum -= nums[left]; left++; } rigth++; } return res == Integer.MAX_VALUE ? 0 : res; }
class Solution: def minSubArrayLen(self, target: int, nums: List[int]) -> int: left, right = 0, 0 res, sum = len(nums) + 1, 0 while right < len(nums): sum += nums[right] while sum >= target: res = min(res, right - left + 1) sum -= nums[left] left += 1 right += 1 return res if res < len(nums) else 0
题目
找出字符串中不含有重复字符的最长子串长度。
分析
还是滑动窗口解决。
代码
public static int lengthOfLongestSubstring1(String s) { // 字符串转为数组方便遍历 char[] chars = s.toCharArray(); // 定义左右指针 int left = 0; int rigth = 0; // 定义res保存不重复的数字的最大长度,初始值设为0; int res = 0; // 定义一个set集合,判断是否有重复字母 Set<Character> set = new HashSet<>(); // 外层循环,右指针移动 while (rigth < chars.length) { // 内层循环,左指针移动 // 如果set中已经包含了即将右指针指的字符,说明这个字符已经有了,出现了重复 // 重复了,就要把重复的字符移除掉,然后继续向右遍历 while (set.contains(chars[rigth])) { set.remove(chars[left]); left++; } // 已经不包含重复的了,因此可以将右指针的字符放入 set.add(chars[rigth]); // 计算滑动窗口长度 res = Math.max(res, rigth - left + 1); rigth++; } return res; }
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: my_set = set() left, right = 0, 0 res = 0 while right < len(s): while s[right] in my_set: res = max(res, right - left) my_set.remove(s[left]) left += 1 my_set.add(s[right]) right += 1 if len(my_set) > res: res = len(my_set) return res
题目
给一个01的数组,和一个整数K,求允许反转K个0后,返回数组中连续为1的最大个数。
分析
求连续为1的最大个数,很明显用滑动窗口,关键是滑动条件是怎么样的?
题目允许最多反转K个0为1,那么就可以定义一个计数器,记录反转的数量,右指针指向的位置,遇到0,计数器加1,左指针指向的位置,遇到0,计数器减一
循环条件:外层循环,右指针不超过边界,不断移动右指针
内层循环,计数器大于K,不断移动左指针
代码
public static int longestOnes1(int[] nums, int k) { // 定义左右指针 int left = 0; int rigth = 0; // 定义最终返回结果,(求最大值就初始化一个小值,求最小值就初始化一个大值) int res = 0; // 定义计数器,计算的是从0变成1的个数 int count=0; // 外层循环 while (rigth < nums.length) { // 遇到右指针指向0,就要变成1,count加1 if (nums[rigth] == 0) { count++; } // 变成1的值多了,就要想办法减少 //开启内存循环 while (count > k) { // 左指针指向0的时候,计数器才可以减一 if (nums[left] == 0) { count--; } left++; } // 循环过程中去res的大值 res = Math.max(res, rigth - left +1); rigth++; } return res; }
class Solution: def longestOnes(self, nums: List[int], k: int) -> int: left, right, res = 0, 0, 0 while right < len(nums): if nums[right] == 0: k -= 1 while k < 0: if nums[left] == 0: k += 1 left += 1 res = max(res, right - left) right += 1 return res
题目
求字符串可以转化的最大长度
分析
字符之间的转换的开销是|s[i] - t[i]|,因此只要计算出每个对应位置的开销,就可以转为最基本的滑动窗口问题。
代码
- public static int equalSubstring1(String s, String t, int maxCost) {
- // 定义一个新的数组,存放两个字符串对应位置转换的开销
- char[] sChars = s.toCharArray();
- char[] tChars = t.toCharArray();
- int[] costs = new int[sChars.length];
- for (int i = 0; i < sChars.length; i++) {
- costs[i] = Math.abs(sChars[i] - tChars[i]);
- }
-
- // 后续就是滑动窗口最基本的解题套路
- int left = 0;
- int right = 0;
- int result = 0;
- int cost = 0;
-
- while (right< costs.length) {
- cost += costs[right];
- while (cost > maxCost) {
- cost -= costs[left];
- left++;
- }
- result = Math.max(result, right - left +1);
- right++;
- }
- return result;
- }

- class Solution:
- def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
- left, right, res, cost = 0, 0, 0, 0
-
- def my_cost(i):
- return abs(ord(s[i]) - ord(t[i]))
-
- while right < len(s):
- cost += my_cost(right)
- while cost > maxCost:
- cost -= my_cost(left)
- left += 1
- res = max(res, right - left + 1)
- right += 1
- return res
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。