题库chevron_right分类chevron_right滑动窗口
view_carousel

滑动窗口

133 道题目
简单: 18中等: 78困难: 37

滑动窗口 是技术面试里最常出现的能力维度之一。建议先掌握基础题型的边界处理,再逐步过渡到模式识别和复杂度 trade-off。

面试场景

高频考察问题建模、边界条件与口头表达的清晰度。

常见误区

只背模板不解释为什么,容易在追问里失分。

练习策略

每轮练 3-5 题,固定复盘复杂度和可替代解法。

推荐练习顺序

#题目难度
3

无重复字符的最长子串

Find the length of the longest substring without repeating characters using a sliding window and hash map to track state…

中等
30

串联所有单词的子串

Find all starting indices of substrings in a string that are concatenations of a given list of words.

困难
76

最小覆盖子串

Find the smallest substring of s containing all characters from t using a sliding window with running state updates for …

困难
187

重复的DNA序列

Solve Repeated DNA Sequences by sliding a length-10 window and tracking seen patterns with a hash set or bitmask.

中等
209

长度最小的子数组

Find the minimal length of a subarray whose sum is greater than or equal to the target using efficient algorithms.

中等
219

存在重复元素 II

Check if any two equal numbers exist within k indices using array scanning and hash table lookup efficiently.

简单
220

存在重复元素 III

The problem involves finding a pair of indices in an array where the index and value differences are within given limits…

困难
239

滑动窗口最大值

Solve the "Sliding Window Maximum" problem using efficient techniques like the sliding window, deque, and priority queue…

困难
395

至少有 K 个重复字符的最长子串

Find the length of the longest substring where every character appears at least k times using sliding window and divide-…

中等
413

等差数列划分

Count the number of arithmetic subarrays in a given integer array using dynamic programming.

中等
424

替换后的最长重复字符

Find the length of the longest substring after at most k replacements using a sliding window and character count trackin…

中等
438

找到字符串中所有字母异位词

Find all starting indices of p's anagrams in s using sliding window and hash table approach.

中等
480

滑动窗口中位数

Compute the median for each sliding window of size k in an array using efficient array scanning and hash lookup techniqu…

困难
567

字符串的排列

Check if a string contains a permutation of another string using two-pointer scanning and hash table techniques.

中等
594

最长和谐子序列

Find the length of the longest harmonious subsequence in an integer array using array scanning and hash-based frequency …

简单
632

最小区间

Find the minimal range covering at least one number from each of k sorted lists using array scanning and hash lookup eff…

困难
643

子数组最大平均数 I

Find the maximum average of any contiguous subarray of length k in a given integer array using a sliding window approach…

简单
658

找到 K 个最接近的元素

Identify the k integers closest to a target x in a sorted array using binary search and two-pointer strategies efficient…

中等
689

三个无重叠子数组的最大和

Maximize the sum of three non-overlapping subarrays with length k in an integer array using dynamic programming.

困难
713

乘积小于 K 的子数组

Count subarrays with a product strictly less than a given value k using efficient algorithms like binary search and slid…

中等
718

最长重复子数组

Find the maximum length of a subarray that appears in both given integer arrays using dynamic programming.

中等
837

新 21 点

Calculate the probability Alice reaches at most n points using state transition dynamic programming efficiently.

中等
862

和至少为 K 的最短子数组

Find the shortest subarray with a sum of at least k using binary search and sliding window techniques.

困难
904

水果成篮

The Fruit Into Baskets problem requires finding the maximum number of fruits you can pick from a row of trees under spec…

中等
930

和相同的二元子数组

Count all contiguous subarrays in a binary array whose elements sum exactly to a given goal using prefix sums efficientl…

中等
978

最长湍流子数组

Find the length of the longest subarray where element comparisons alternate, using state transition dynamic programming …

中等
992

K 个不同整数的子数组

Find subarrays with exactly k distinct integers in an integer array using sliding window and hash lookup techniques.

困难
995

K 连续位的最小翻转次数

Determine the minimum number of k-length consecutive bit flips needed to convert all zeros to ones in a binary array eff…

困难
1004

最大连续1的个数 III

Find the longest consecutive ones in a binary array by optimally flipping at most k zeros using sliding window technique…

中等
1016

子串能表示从 1 到 N 数字的二进制串

Check if binary string contains all integers from 1 to n as substrings, leveraging sliding window and bit manipulation t…

中等
1031

两个无重叠子数组的最大和

Find the maximum sum of two non-overlapping subarrays by efficiently using state transition dynamic programming.

中等
1040

移动石子直到连续 II

Determine the minimum and maximum moves to make stones consecutive using sliding window and endpoint adjustments efficie…

中等
1044

最长重复子串

Find the longest duplicated substring in a string using binary search, sliding window, and rolling hash techniques.

困难
1052

爱生气的书店老板

Maximize satisfied customers in a bookstore by strategically suppressing the owner's grumpy minutes using a sliding wind…

中等
1156

单字符重复子串的最大长度

Find the maximum length of a repeated character substring after swapping two characters using a sliding window approach …

中等
1208

尽可能使字符串相等

Optimize substring transformations using a binary search approach to maximize the change within a budget.

中等
1234

替换子串得到平衡字符串

Determine the minimum substring length to replace in order to balance a string of Q, W, E, and R characters efficiently.

中等
1248

统计「优美子数组」

The problem requires counting subarrays with exactly k odd numbers using an efficient array scanning approach.

中等
1297

子串的最大出现次数

Find the maximum number of occurrences of any valid substring in a given string with specific constraints on letter coun…

中等
1343

大小为 K 且平均值大于等于阈值的子数组数目

Given an array, find the number of sub-arrays of size k with an average greater than or equal to a given threshold.

中等
1358

包含所有三种字符的子字符串数目

Count all substrings containing at least one of each character a, b, and c using a sliding window approach efficiently.

中等
1423

可获得的最大点数

Maximize your score by selecting k cards from the beginning or end of the array using a sliding window approach.

中等
1425

带限制的子序列和

Solve the Constrained Subsequence Sum problem using dynamic programming, sliding window, and priority queues to maximize…

困难
1438

绝对差不超过限制的最长连续子数组

Find the longest subarray with elements whose absolute difference is within a specified limit using a sliding window app…

中等
1456

定长子串中元音的最大数目

Find the maximum number of vowels in a substring of a given length in a string using a sliding window approach.

中等
1477

找两个和为目标值且不重叠的子数组

Find two non-overlapping sub-arrays with a given target sum and return the minimal total length efficiently using array …

中等
1493

删掉一个元素以后全为 1 的最长子数组

Solve LeetCode 1493 by tracking runs of 1s around one deletion using a tight sliding window or state transition DP.

中等
1499

满足不等式的最大值

Max Value of Equation asks to find the maximum value of a specific equation on a set of 2D points using sliding window t…

困难
1610

可见点的最大数目

Determine the maximum number of points visible from a fixed location within a given angle using a sliding window approac…

困难
1652

拆炸弹

Defuse the Bomb uses a sliding window to update a circular array based on a key, with efficient state updates.

简单
1658

将 x 减到 0 的最小操作数

Find the minimum number of operations to reduce a value x to zero by removing elements from an array.

中等
1695

删除子数组的最大得分

Maximize the score by erasing a subarray with unique elements in an array of integers.

中等
1703

得到连续 K 个 1 的最少相邻交换次数

Find the minimum number of adjacent swaps to gather k consecutive ones in a binary array using sliding window logic.

困难
1763

最长的美好子字符串

Find the longest nice substring in a given string using the sliding window technique with running state updates.

简单
1838

最高频元素的频数

Maximize the frequency of an element in an array by incrementing at most `k` elements. Use binary search and greedy tech…

中等
1839

所有元音按顺序排布的最长子字符串

Find the longest substring of all vowels in order within a given string of vowels using sliding window technique.

中等
1871

跳跃游戏 VII

The problem asks to determine if we can reach the last index of a binary string, with a set range of jumps between indic…

中等
1876

长度为三且各字符不同的子字符串

Count all substrings of length three in a string where each character is unique using sliding window techniques efficien…

简单
1888

使二进制字符串字符交替的最少反转次数

Find the minimum number of flips to make a binary string alternate, using state transition dynamic programming.

中等
1984

学生分数的最小差值

Minimize the difference between the highest and lowest of k student scores using a sliding window approach.

简单
2009

使数组连续的最少操作数

Find the minimum number of operations to make an array continuous by modifying elements using array scanning and hash lo…

困难
2024

考试的最大困扰度

Maximize the Confusion of an Exam requires adjusting at most k answers to create the longest consecutive true or false s…

中等
2090

半径为 k 的子数组平均值

Efficiently calculate the k-radius average for subarrays using sliding window technique.

中等
2106

摘水果

Compute the maximum fruits collectable on an infinite line by moving at most k steps from a start position efficiently.

困难
2134

最少交换次数来组合所有的 1 II

Solve the minimum swaps to group all 1's together in a circular binary array using an optimized sliding window approach.

中等
2156

查找给定哈希值的子串

Locate the first substring of length k whose rolling hash matches the given hashValue using a sliding window approach.

困难
2260

必须拿起的最小连续卡牌数

Solve Minimum Consecutive Cards to Pick Up by tracking each card's last index and minimizing duplicate spans during one …

中等
2269

找到一个数字的 K 美丽值

Calculate the k-beauty of a number by counting substrings of length k that divide the original number evenly using a sli…

简单
2271

毯子覆盖的最多白色砖块数

Solve Maximum White Tiles Covered by a Carpet by sorting intervals, checking optimal carpet starts, and counting full pl…

中等
2302

统计得分小于 K 的子数组数目

Count all non-empty subarrays whose score, defined as sum times length, is strictly less than a given integer k efficien…

困难
2379

得到 K 个黑块的最少涂色次数

Given a string of blocks, determine the minimum number of recolors needed to get k consecutive black blocks.

简单
2398

预算内的最多机器人数目

Determine the maximum number of consecutive robots you can operate without exceeding a given budget using efficient bina…

困难
2401

最长优雅子数组

Find the length of the longest nice subarray where the bitwise AND of all pairs of elements in the subarray is zero.

中等
2411

按位或最大的最小子数组长度

Find the smallest subarrays with the maximum bitwise OR for each starting index in an array.

中等
2444

统计定界子数组的数目

Count all subarrays where the minimum and maximum match given bounds using efficient sliding window tracking techniques.

困难
2461

长度为 K 子数组中的最大和

Find the maximum sum of all distinct-element subarrays of length k using array scanning and hash lookup techniques.

中等
2516

每种字符至少取 K 个

Find the minimum number of minutes needed to take at least k of each character from both ends of a string.

中等
2528

最大化城市的最小电量

Determine the maximum minimum power a city can achieve by strategically adding power stations using binary search and pr…

困难
2537

统计好子数组的数目

Count the number of subarrays that contain at least k pairs of equal elements using array scanning and hash lookup.

中等
2555

两个线段获得的最多奖品

Maximize the total prizes by choosing two segments of length k on a sorted line of prize positions efficiently using bin…

中等
2653

滑动子数组的美丽值

Compute the xth smallest negative number in each sliding subarray using array scanning and hash frequency tracking effic…

中等
2730

找到最长的半重复子字符串

Find the length of the longest substring where at most one adjacent pair of digits repeats, using a sliding window appro…

中等
2747

统计没有收到请求的服务器数目

Count Zero Request Servers finds the number of servers with zero requests during specific time intervals for a given set…

中等
2760

最长奇偶子数组

Determine the length of the longest subarray where elements alternate even and odd while staying below a given threshold…

简单
2762

不间断子数组

Count all continuous subarrays efficiently using sliding window with running max-min state tracking for array consistenc…

中等
2779

数组的最大美丽值

Find the maximum beauty of an array by adjusting elements within a range using binary search and sliding window techniqu…

中等
2781

最长合法子字符串的长度

This problem asks for the longest valid substring of a word that doesn't contain any substrings from a forbidden list.

困难
2799

统计完全子数组的数目

Count Complete Subarrays in an Array requires scanning the array while tracking elements to detect subarrays with all di…

中等
2831

找出最长等值子数组

Find the maximum length of an equal subarray after removing up to k elements using array scanning and hash-based index t…

中等
2841

几乎唯一子数组的最大和

Find the maximum sum of subarrays that contain at least m distinct elements using array scanning and hash lookups effici…

中等
2875

无限数组的最短子数组

Find the shortest subarray in an infinite array that sums to a given target using array scanning and hash lookup.

中等
2902

和带限制的子多重集合的数目

This problem asks you to count the number of sub-multisets within a given array that have a sum in a specified range.

困难
2904

最短且字典序最小的美丽子字符串

Find the shortest beautiful substring in a binary string and return the lexicographically smallest option efficiently us…

中等
2932

找出强数对的最大异或值 I

Find the maximum XOR of any strong pair in an integer array using array scanning and hash-based lookups efficiently.

简单
2935

找出强数对的最大异或值 II

Find the maximum XOR among strong pairs in an array using array scanning and hash-based lookups efficiently.

困难
2953

统计完全子字符串

Count Complete Substrings involves finding substrings where each character appears exactly k times with constraints on a…

困难
2958

最多 K 个重复元素的最长子数组

Find the maximum length subarray where each number appears at most k times using array scanning and hash lookups.

中等
2962

统计最大元素出现至少 K 次的子数组

Find the number of subarrays where the maximum element appears at least k times using a sliding window approach.

中等
2968

执行操作使频率分数最大

Maximize the frequency score by applying up to k operations on a sorted array using binary search over valid answer spac…

困难
2981

找出出现至少三次的最长特殊子字符串 I

Find the longest special substring in a string that appears at least three times, or return -1 if no such substring exis…

中等
2982

找出出现至少三次的最长特殊子字符串 II

Find the longest special substring that occurs at least three times in a given string using binary search and hash table…

中等
3013

将数组分成最小总代价的子数组 II

This problem asks to divide an array into subarrays with a minimal cost and certain constraints on subarray positions.

困难
3086

拾起 K 个 1 需要的最少行动次数

Find the minimum number of moves to pick exactly k ones from a binary array, considering a constraint on changes.

困难
3090

每个字符最多出现两次的最长子字符串

Find the maximum length substring where some substring occurs at least twice using a sliding window and hash mapping str…

简单
3095

或值至少 K 的最短子数组 I

Find the shortest non-empty subarray in nums whose bitwise OR reaches at least k using sliding window efficiently.

简单
3097

或值至少为 K 的最短子数组 II

Find the length of the shortest subarray where the bitwise OR of all its elements is at least a given value.

中等
3134

找出唯一性数组的中位数

Given a nums array, find the median of its uniqueness array by considering all subarrays and their distinct element coun…

困难
3191

使二进制数组全部等于 1 的最少操作次数 I

Find the minimum number of operations to make all elements of a binary array equal to one using sliding window and state…

中等
3206

交替组 I

Count all alternating groups in a circular array by tracking tiles with distinct neighbors efficiently using sliding win…

简单
3208

交替组 II

Solve the problem of counting alternating groups in a circle of tiles using a sliding window approach.

中等
3234

统计 1 显著的字符串的数量

Count the number of substrings in a binary string with dominant ones, using a sliding window approach with state updates…

中等
3254

长度为 K 的子数组的能量值 I

Given an array, find the power of all subarrays of size k using a sliding window approach.

中等
3255

长度为 K 的子数组的能量值 II

Compute the power of all k-size subarrays in an array using sliding window with running state updates efficiently.

中等
3258

统计满足 K 约束的子字符串数量 I

Count all substrings in a binary string that meet a given k-constraint using an efficient sliding window approach.

简单
3261

统计满足 K 约束的子字符串数量 II

Count Substrings That Satisfy K-Constraint II requires finding valid substrings in a binary string based on a k-constrai…

困难
3297

统计重新排列后包含另一个字符串的子字符串数目 I

Count the number of valid substrings in word1 that can be rearranged to contain word2 as a prefix.

中等
3298

统计重新排列后包含另一个字符串的子字符串数目 II

Count Substrings That Can Be Rearranged to Contain a String II involves identifying valid substrings with a sliding wind…

困难
3305

元音辅音字符串计数 I

Count all substrings containing every vowel and exactly k consonants using a sliding window and hash map state tracking.

中等
3306

元音辅音字符串计数 II

Count the number of substrings containing all vowels and exactly k consonants using a sliding window technique.

中等
3318

计算子数组的 x-sum I

Compute the x-sum of every subarray of length k efficiently using array scanning combined with hash lookup techniques.

简单
3321

计算子数组的 x-sum II

Calculate the x-sum for every k-length subarray using efficient array scanning and hash-based counting techniques.

困难
3325

字符至少出现 K 次的子字符串 I

Calculate the total number of substrings where at least one character repeats k times using a sliding window efficiently…

中等
3346

执行操作后元素的最高频率 I

Maximize the frequency of an element in an array after performing a series of operations to find the best possible resul…

中等
3347

执行操作后元素的最高频率 II

Determine the maximum frequency of any element after performing limited operations using binary search and sliding windo…

困难
3364

最小正和子数组

Find the minimum sum of any subarray of size between l and r with a positive total using efficient sliding window logic.

简单
3411

最长乘积等价子数组

This problem involves finding the longest subarray where the product equals the LCM multiplied by the GCD, leveraging a …

简单
3413

收集连续 K 个袋子可以获得的最多硬币数量

Solve the problem of maximizing coins from selecting k consecutive bags, using binary search and sliding window techniqu…

中等
3420

统计 K 次操作以内得到非递减子数组的数目

This problem asks you to count non-decreasing subarrays in a given array after applying at most k operations.

困难
3439

重新安排会议得到最多空余时间 I

Maximize free time by rescheduling up to k non-overlapping meetings within a fixed event using sliding window updates.

中等
3445

奇偶频次间的最大差值 II

Find the maximum difference between even and odd character frequencies in substrings using sliding window updates effici…

困难
3505

使 K 个子数组内元素相等的最少操作数

Compute the minimum operations to ensure at least k non-overlapping subarrays of size x have all equal elements efficien…

困难
3578

统计极差最大为 K 的分割方式数

Count the number of valid ways to partition an array into contiguous segments where max-min difference is at most k.

中等
3589

计数质数间隔平衡子数组

Count the number of prime-gap balanced subarrays in an integer array using sliding window techniques and running state u…

中等

关联高频模式

LeetCode 滑动窗口题型题解:133题训练路线