阅读设置
界面、侧栏和正文分别调整,自动保存。
主题切换
界面、侧栏和正文分别调整,自动保存。
滑动窗口是双指针的一种高级用法,专门用来解决数组/字符串的连续子序列问题。
想象一个窗口在数组上从左向右滑动:
[left, right]核心思想:固定一个指针,移动另一个指针,动态维护窗口内的数据,避免重复计算。
凡是题目出现这些关键词,优先想滑动窗口:
// 滑动窗口万能模板
public int slidingWindow(int[] nums) {
int left = 0; // 左指针
int max = 0; // 结果
// 右指针一直往前走,遍历整个数组
for (int right = 0; right < nums.length; right++) {
// 1. 把当前元素加入窗口
// 2. 判断窗口是否需要收缩(不满足条件时)
while (窗口 不合法 / 超标) {
// 移除 left 位置元素
left++;
}
// 3. 此时窗口合法,更新答案
max = Math.max(max, right - left + 1);
}
return max;
}题目:给定一个字符串,返回无重复字符的最长连续子串长度。
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<>();
int left = 0;
int max = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
// 有重复,收缩左边界
while (set.contains(c)) {
set.remove(s.charAt(left));
left++;
}
set.add(c);
max = Math.max(max, right - left + 1);
}
return max;
}题目:找出数组中和 ≥ target 的长度最小的连续子数组。
public int minSubArrayLen(int target, int[] nums) {
int left = 0;
int sum = 0;
int min = Integer.MAX_VALUE;
for (int right = 0; right < nums.length; right++) {
sum += nums[right];
// 满足条件,尝试缩小窗口
while (sum >= target) {
min = Math.min(min, right - left + 1);
sum -= nums[left];
left++;
}
}
return min == Integer.MAX_VALUE ? 0 : min;
}题目:给定一个数组和窗口大小 k,输出每个窗口的最大值。
public int[] maxSlidingWindow(int[] nums, int k) {
Deque<Integer> deque = new LinkedList<>();
int[] res = new int[nums.length - k + 1];
int index = 0;
for (int right = 0; right < nums.length; right++) {
// 维护递减队列
while (!deque.isEmpty() && nums[right] > nums[deque.peekLast()]) {
deque.pollLast();
}
deque.offerLast(right);
// 超出窗口左边界,移除
while (deque.peekFirst() <= right - k) {
deque.pollFirst();
}
// 窗口形成,记录最大值
if (right >= k - 1) {
res[index++] = nums[deque.peekFirst()];
}
}
return res;
}