题库chevron_right双·指针·invariant

双·指针·invariant 模式

99 道题目

模式页适合用来建立可复用解题框架。先识别题目特征,再按固定流程解释状态定义、转移和边界。

识别信号

  • Do you understand the two-pointer technique and how it helps reduce the time complexity of this problem?
  • Can you explain why we always move the pointer pointing to the shorter line in the two-pointer approach?
  • Do you see how sorting simplifies duplicate management in two-pointer scanning?

解题流程

  1. 1. 明确窗口/状态定义
  2. 2. 更新状态并维护约束
  3. 3. 用边界样例验证

常见失分点

  • Failing to optimize the solution and instead using a brute-force approach that simulates all pairs, leading to O(n^2) time complexity.
  • Failing to skip duplicate fixed elements leads to repeated triplets in output.
  • Forgetting to sort the array first, which can lead to inefficient solutions or missed results.

推荐题单梯度

#题目难度
11

盛最多水的容器

Find two vertical lines that can form a container with the most water in a given array of heights.

中等
15

三数之和

Given an integer array, return all unique triplets where the sum is zero using two-pointer scanning with careful duplica…

中等
16

最接近的三数之和

Find the sum of three integers in an array that is closest to a given target using two-pointer scanning.

中等
18

四数之和

The 4Sum problem requires finding all unique quadruplets in an array that sum to a specific target value.

中等
26

删除有序数组中的重复项

Learn how to remove duplicates from a sorted array in-place using two-pointer scanning while preserving element order.

简单
27

移除元素

Remove Element challenges you to remove a value from an array in-place using efficient two-pointer scanning and tracking…

简单
28

找出字符串中第一个匹配项的下标

Locate the first occurrence of a substring within a string using a two-pointer scanning strategy and invariant tracking …

简单
31

下一个排列

Next Permutation is a medium-difficulty problem focusing on generating the next lexicographically greater permutation of…

中等
75

颜色分类

Sort Colors requires in-place reordering of an array using a two-pointer scanning strategy to group 0s, 1s, and 2s effic…

中等
80

删除有序数组中的重复项 II

Solve the problem of removing duplicates from a sorted array in-place, ensuring each element appears at most twice, usin…

中等
88

合并两个有序数组

Merge two sorted arrays into the first array in non-decreasing order, using a two-pointer approach.

简单
125

验证回文串

Check if a given string is a valid palindrome by using two-pointer scanning and invariant tracking techniques.

简单
151

反转字符串中的单词

Reverse Words in a String requires reordering words using precise two-pointer scanning with careful space handling for e…

中等
165

比较版本号

Compare two version numbers by checking their individual revisions, considering missing revisions as zero, using a two-p…

中等
189

轮转数组

Rotate Array challenges you to shift elements right by k steps using precise two-pointer scanning and invariant tracking…

中等
202

快乐数

Determine if a number is happy by repeatedly summing squares of digits using two-pointer scanning to detect cycles effic…

简单
283

移动零

Move all zeros in an array to the end while preserving the order of non-zero elements using efficient in-place operation…

简单
295

数据流的中位数

Implement a MedianFinder class that supports adding numbers and finding the median from a data stream.

困难
321

拼接最大数

Create Maximum Number involves merging digits from two arrays while preserving order, maximizing the resulting number.

困难
344

反转字符串

Reverse a character array in-place using a two-pointer scanning technique, ensuring minimal memory and correct index swa…

简单
345

反转字符串中的元音字母

Reverse Vowels of a String uses a two-pointer approach to reverse the vowels in a string while leaving the consonants in…

简单
443

压缩字符串

Compress a character array in-place by converting consecutive repeated characters into counts using two-pointer scanning…

中等
455

分发饼干

Maximize content children by assigning at most one cookie per child using two-pointer scanning and greedy sorting techni…

简单
481

神奇字符串

Count the number of '1's in the first n characters of a magical string using two-pointer scanning and invariant tracking…

中等
524

通过删除字母匹配到字典里最长单词

Find the longest word in the dictionary that can be formed by deleting characters from a string, using two-pointer scann…

中等
541

反转字符串 II

Reverse String II requires reversing segments of a string using two-pointer scanning while tracking invariants carefully…

简单
556

下一个更大元素 III

Find the next greater integer using the same digits as a given number with precise two-pointer scanning and invariant tr…

中等
557

反转字符串中的单词 III

Reverse Words in a String III involves reversing each word in a sentence using the two-pointer technique.

简单
567

字符串的排列

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

中等
581

最短无序连续子数组

Find the shortest unsorted continuous subarray that, if sorted, would sort the entire array.

中等
680

验证回文串 II

Check if a string can become a palindrome by deleting at most one character using two-pointer scanning and invariant tra…

简单
696

计数二进制子串

Count Binary Substrings solves for the number of valid substrings with equal 0's and 1's, grouped consecutively in a bin…

简单
763

划分字母区间

Partition a string into maximal parts so that each letter appears in only one segment, using two-pointer scanning.

中等
777

在 LR 字符串中交换相邻字符

Transform one string into another by swapping adjacent 'L' and 'R' characters in a sequence of moves.

中等
795

区间子数组个数

Count the number of contiguous subarrays with a bounded maximum value using a two-pointer approach.

中等
809

情感丰富的文字

Expressive Words challenges you to determine if a word can be transformed into a given string by extending groups of rep…

中等
821

字符的最短距离

Compute the minimum distance from each character in a string to a target character using efficient scanning techniques.

简单
832

翻转图像

Flip each row of a binary matrix horizontally and invert its values efficiently using a two-pointer scanning approach.

简单
844

比较含退格的字符串

Compare two strings after processing backspaces using efficient two-pointer scanning and careful invariant tracking to e…

简单
870

优势洗牌

Maximize the advantage of nums1 over nums2 using a two-pointer greedy strategy, carefully tracking which elements beat o…

中等
881

救生艇

Find the minimum number of boats required to save all people, using a two-pointer approach for efficient pairing.

中等
905

按奇偶排序数组

Reorder an integer array so all even numbers come before odd numbers using a precise two-pointer scanning method.

简单
917

仅仅反转字母

Reverse the characters of a string while skipping non-letter characters using a two-pointer technique.

简单
922

按奇偶排序数组 II

Sort an array where even numbers appear at even indices and odd numbers appear at odd indices using two-pointer scanning…

简单
925

长按键入

Check if a typed string could result from long pressing characters while typing a given name using a two-pointer scan.

简单
942

增减字符串匹配

Reconstruct a permutation from a DI string using two-pointer scanning, carefully tracking the increasing and decreasing …

简单
948

令牌放置

Maximize your score in the Bag of Tokens problem by strategically playing tokens with two-pointer scanning and greedy te…

中等
962

最大宽度坡

Find the maximum width of a ramp where nums[i] <= nums[j] for i < j using a two-pointer approach.

中等
969

煎饼排序

Sort an array using pancake flips, leveraging two-pointer scanning and invariant tracking to iteratively position the la…

中等
977

有序数组的平方

The problem involves squaring elements of a sorted array and returning the squares in non-decreasing order.

简单
986

区间列表的交集

This problem requires finding the intersection of two lists of intervals using a two-pointer technique with invariant tr…

中等
1023

驼峰式匹配

Camelcase Matching is a medium difficulty problem where you match queries to a given pattern by inserting lowercase lett…

中等
1089

复写零

Duplicate each zero in a fixed-length array in place, shifting elements right using a two-pointer scanning approach effi…

简单
1163

按字典序排在最后的子串

Identify the lexicographically last substring in a string using two-pointer scanning with invariant tracking efficiently…

困难
1332

删除回文子序列

This problem challenges you to remove palindromic subsequences from a string with a minimum number of steps using two-po…

简单
1455

检查单词是否为句中其他单词的前缀

Determine the first word in a sentence where a given searchWord is a prefix using two-pointer scanning with string match…

简单
1471

数组中的 k 个最强值

Identify the k strongest values in an array using two-pointer scanning and careful tracking of the array's centre value.

中等
1616

分割两个字符串得到回文串

Determine if splitting two equal-length strings at a common index can form a palindrome using two-pointer scanning techn…

中等
1697

检查边长度限制的路径是否存在

Solve the problem of checking if there exists a path between two nodes under edge length constraints using efficient tec…

困难
1750

删除字符串两端相同字符后的最短长度

Minimize string length by deleting similar characters from both ends repeatedly using a two-pointer technique.

中等
1754

构造字典序最大的合并字符串

Construct the lexicographically largest merge from two strings using a two-pointer greedy scanning approach efficiently.

中等
1764

通过连接另一个数组的子数组得到一个数组

Determine if you can sequentially select disjoint subarrays from nums matching each group in order using two-pointer sca…

中等
1768

交替合并字符串

Merge two strings alternately, starting with the first string, and append extra characters when one string is longer.

简单
1813

句子相似性 III

Sentence Similarity III asks if one sentence can be transformed into another by inserting a sentence inside it.

中等
1850

邻位交换的最小次数

Find the minimum number of adjacent swaps to reach the kth smallest wonderful integer from a given number string.

中等
1861

旋转盒子

Rotate a box represented by a character matrix, letting stones fall under gravity using precise two-pointer scanning and…

中等
1877

数组中最大数对和的最小值

Minimize the maximum pair sum in an array by optimally pairing its elements.

中等
1961

检查字符串是否为数组前缀

Determine if a string is a prefix of an array by concatenating initial elements, using two-pointer scanning efficiently.

简单
1963

使字符串平衡的最小交换次数

Determine the minimum swaps to balance a bracket string using two-pointer scanning with invariant tracking efficiently.

中等
2000

反转单词前缀

Reverse the prefix of a string starting from index 0 to the first occurrence of a given character.

简单
2105

给植物浇水 II

Simulate watering a row of plants with Alice and Bob using two-pointer scanning, tracking refills precisely for each ste…

中等
2108

找出数组中的第一个回文字符串

Return the first palindromic string from an array of words or an empty string if none exists.

简单
2109

向字符串添加空格

Learn to efficiently insert spaces in a string using two-pointer scanning with invariant tracking in linear time.

中等
2149

按符号重排数组

Rearrange an array by alternating positive and negative integers using a two-pointer approach with invariant tracking.

中等
2161

根据给定数字划分数组

Rearrange an array around a pivot while maintaining relative order using a two-pointer scanning approach efficiently.

中等
2193

得到回文串的最少操作次数

The problem challenges you to find the minimum number of adjacent swaps to make a string a palindrome.

困难
2200

找出数组中的所有 K 近邻下标

Identify all indices in an array that are within k distance of elements equal to the given key using two-pointer scannin…

简单
2337

移动片段得到字符串

Determine if the pieces in start can be moved to form target using two-pointer scanning and strict left-right movement r…

中等
2396

严格回文的数字

Determine if a number is strictly palindromic in all bases from 2 to n minus 2 using two-pointer scanning and invariant …

中等
2406

将区间分为最少组数

Determine the minimum number of non-overlapping groups for a set of intervals using precise two-pointer scanning logic.

中等
2410

运动员和训练师的最大匹配数

Maximize the number of valid player-trainer matchings using two-pointer scanning and careful sorting strategy.

中等
2460

对数组执行操作

Transform an array by applying adjacent doubling operations and shifting zeros efficiently using two-pointer scanning te…

简单
2462

雇佣 K 位工人的总代价

Optimize the total cost of hiring exactly k workers using a two-pointer approach with invariant tracking and priority qu…

中等
2486

追加字符以获得子序列

Determine the minimum characters to append to s so t becomes a subsequence using efficient two-pointer scanning techniqu…

中等
2503

矩阵查询可获得的最大分数

Solve the Maximum Number of Points From Grid Queries problem using two-pointer scanning and invariant tracking.

困难
2511

最多可以摧毁的敌人城堡数目

Find the maximum number of enemy forts that can be captured by moving your army using two-pointer scanning with invarian…

简单
2562

找出数组的串联值

Compute the array concatenation value efficiently using two-pointer scanning while maintaining a running total of concat…

简单
2592

最大化数组的伟大值

Maximize Greatness of an Array requires permuting numbers to exceed original values at most indices efficiently.

中等
2697

字典序最小回文串

Given a string, make it a palindrome with the fewest operations, prioritizing lexicographically smallest result.

简单
2825

循环增长使字符串子序列等于另一个字符串

Determine if str2 can be made a subsequence of str1 by incrementing characters cyclically using at most one operation.

中等
2903

找出满足差值条件的下标 I

Find two indices in an array where their difference in both index and value meets specified thresholds.

简单
2905

找出满足差值条件的下标 II

Locate two indices in an array where both index and value differences meet specified thresholds using precise scanning t…

中等
2938

区分黑球与白球

Solve the "Separate Black and White Balls" problem by swapping adjacent balls to group all black balls to the right with…

中等
3132

找出与数组相加的整数 II

Given two arrays nums1 and nums2, determine the integer added to nums1 to make it equal to nums2 after removing two elem…

中等
3194

最小元素和最大元素的最小平均值

Solve the Minimum Average of Smallest and Largest Elements problem using two-pointer scanning to track averages effectiv…

简单
3239

最少翻转次数使二进制矩阵回文 I

Find the minimum flips to make a binary grid palindromic by scanning with two pointers and tracking symmetry efficiently…

中等
3240

最少翻转次数使二进制矩阵回文 II

The problem asks to flip cells in a binary grid to make its rows and columns palindromic using the minimum number of fli…

中等
3403

从盒子中找出字典序最大的字符串 I

This problem involves finding the lexicographically largest string from a given word using a two-pointer approach.

中等
3584

子序列首尾元素的最大乘积

Maximize the product of the first and last elements of any subsequence of size m in an integer array.

中等

关联题型

LeetCode 双·指针·invariant模式题解:99题训练路线