识别信号
- 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. 明确窗口/状态定义
- 2. 更新状态并维护约束
- 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.
推荐题单梯度
盛最多水的容器
Find two vertical lines that can form a container with the most water in a given array of heights.
三数之和
Given an integer array, return all unique triplets where the sum is zero using two-pointer scanning with careful duplica…
最接近的三数之和
Find the sum of three integers in an array that is closest to a given target using two-pointer scanning.
四数之和
The 4Sum problem requires finding all unique quadruplets in an array that sum to a specific target value.
删除有序数组中的重复项
Learn how to remove duplicates from a sorted array in-place using two-pointer scanning while preserving element order.
移除元素
Remove Element challenges you to remove a value from an array in-place using efficient two-pointer scanning and tracking…
找出字符串中第一个匹配项的下标
Locate the first occurrence of a substring within a string using a two-pointer scanning strategy and invariant tracking …
下一个排列
Next Permutation is a medium-difficulty problem focusing on generating the next lexicographically greater permutation of…
颜色分类
Sort Colors requires in-place reordering of an array using a two-pointer scanning strategy to group 0s, 1s, and 2s effic…
删除有序数组中的重复项 II
Solve the problem of removing duplicates from a sorted array in-place, ensuring each element appears at most twice, usin…
合并两个有序数组
Merge two sorted arrays into the first array in non-decreasing order, using a two-pointer approach.
验证回文串
Check if a given string is a valid palindrome by using two-pointer scanning and invariant tracking techniques.
反转字符串中的单词
Reverse Words in a String requires reordering words using precise two-pointer scanning with careful space handling for e…
比较版本号
Compare two version numbers by checking their individual revisions, considering missing revisions as zero, using a two-p…
轮转数组
Rotate Array challenges you to shift elements right by k steps using precise two-pointer scanning and invariant tracking…
快乐数
Determine if a number is happy by repeatedly summing squares of digits using two-pointer scanning to detect cycles effic…
移动零
Move all zeros in an array to the end while preserving the order of non-zero elements using efficient in-place operation…
数据流的中位数
Implement a MedianFinder class that supports adding numbers and finding the median from a data stream.
拼接最大数
Create Maximum Number involves merging digits from two arrays while preserving order, maximizing the resulting number.
反转字符串
Reverse a character array in-place using a two-pointer scanning technique, ensuring minimal memory and correct index swa…
反转字符串中的元音字母
Reverse Vowels of a String uses a two-pointer approach to reverse the vowels in a string while leaving the consonants in…
压缩字符串
Compress a character array in-place by converting consecutive repeated characters into counts using two-pointer scanning…
分发饼干
Maximize content children by assigning at most one cookie per child using two-pointer scanning and greedy sorting techni…
神奇字符串
Count the number of '1's in the first n characters of a magical string using two-pointer scanning and invariant tracking…
通过删除字母匹配到字典里最长单词
Find the longest word in the dictionary that can be formed by deleting characters from a string, using two-pointer scann…
反转字符串 II
Reverse String II requires reversing segments of a string using two-pointer scanning while tracking invariants carefully…
下一个更大元素 III
Find the next greater integer using the same digits as a given number with precise two-pointer scanning and invariant tr…
反转字符串中的单词 III
Reverse Words in a String III involves reversing each word in a sentence using the two-pointer technique.
字符串的排列
Check if a string contains a permutation of another string using two-pointer scanning and hash table techniques.
最短无序连续子数组
Find the shortest unsorted continuous subarray that, if sorted, would sort the entire array.
验证回文串 II
Check if a string can become a palindrome by deleting at most one character using two-pointer scanning and invariant tra…
计数二进制子串
Count Binary Substrings solves for the number of valid substrings with equal 0's and 1's, grouped consecutively in a bin…
划分字母区间
Partition a string into maximal parts so that each letter appears in only one segment, using two-pointer scanning.
在 LR 字符串中交换相邻字符
Transform one string into another by swapping adjacent 'L' and 'R' characters in a sequence of moves.
区间子数组个数
Count the number of contiguous subarrays with a bounded maximum value using a two-pointer approach.
情感丰富的文字
Expressive Words challenges you to determine if a word can be transformed into a given string by extending groups of rep…
字符的最短距离
Compute the minimum distance from each character in a string to a target character using efficient scanning techniques.
翻转图像
Flip each row of a binary matrix horizontally and invert its values efficiently using a two-pointer scanning approach.
比较含退格的字符串
Compare two strings after processing backspaces using efficient two-pointer scanning and careful invariant tracking to e…
优势洗牌
Maximize the advantage of nums1 over nums2 using a two-pointer greedy strategy, carefully tracking which elements beat o…
救生艇
Find the minimum number of boats required to save all people, using a two-pointer approach for efficient pairing.
按奇偶排序数组
Reorder an integer array so all even numbers come before odd numbers using a precise two-pointer scanning method.
仅仅反转字母
Reverse the characters of a string while skipping non-letter characters using a two-pointer technique.
按奇偶排序数组 II
Sort an array where even numbers appear at even indices and odd numbers appear at odd indices using two-pointer scanning…
长按键入
Check if a typed string could result from long pressing characters while typing a given name using a two-pointer scan.
增减字符串匹配
Reconstruct a permutation from a DI string using two-pointer scanning, carefully tracking the increasing and decreasing …
令牌放置
Maximize your score in the Bag of Tokens problem by strategically playing tokens with two-pointer scanning and greedy te…
最大宽度坡
Find the maximum width of a ramp where nums[i] <= nums[j] for i < j using a two-pointer approach.
煎饼排序
Sort an array using pancake flips, leveraging two-pointer scanning and invariant tracking to iteratively position the la…
有序数组的平方
The problem involves squaring elements of a sorted array and returning the squares in non-decreasing order.
区间列表的交集
This problem requires finding the intersection of two lists of intervals using a two-pointer technique with invariant tr…
驼峰式匹配
Camelcase Matching is a medium difficulty problem where you match queries to a given pattern by inserting lowercase lett…
复写零
Duplicate each zero in a fixed-length array in place, shifting elements right using a two-pointer scanning approach effi…
按字典序排在最后的子串
Identify the lexicographically last substring in a string using two-pointer scanning with invariant tracking efficiently…
删除回文子序列
This problem challenges you to remove palindromic subsequences from a string with a minimum number of steps using two-po…
检查单词是否为句中其他单词的前缀
Determine the first word in a sentence where a given searchWord is a prefix using two-pointer scanning with string match…
数组中的 k 个最强值
Identify the k strongest values in an array using two-pointer scanning and careful tracking of the array's centre value.
分割两个字符串得到回文串
Determine if splitting two equal-length strings at a common index can form a palindrome using two-pointer scanning techn…
检查边长度限制的路径是否存在
Solve the problem of checking if there exists a path between two nodes under edge length constraints using efficient tec…
删除字符串两端相同字符后的最短长度
Minimize string length by deleting similar characters from both ends repeatedly using a two-pointer technique.
构造字典序最大的合并字符串
Construct the lexicographically largest merge from two strings using a two-pointer greedy scanning approach efficiently.
通过连接另一个数组的子数组得到一个数组
Determine if you can sequentially select disjoint subarrays from nums matching each group in order using two-pointer sca…
交替合并字符串
Merge two strings alternately, starting with the first string, and append extra characters when one string is longer.
句子相似性 III
Sentence Similarity III asks if one sentence can be transformed into another by inserting a sentence inside it.
邻位交换的最小次数
Find the minimum number of adjacent swaps to reach the kth smallest wonderful integer from a given number string.
旋转盒子
Rotate a box represented by a character matrix, letting stones fall under gravity using precise two-pointer scanning and…
数组中最大数对和的最小值
Minimize the maximum pair sum in an array by optimally pairing its elements.
检查字符串是否为数组前缀
Determine if a string is a prefix of an array by concatenating initial elements, using two-pointer scanning efficiently.
使字符串平衡的最小交换次数
Determine the minimum swaps to balance a bracket string using two-pointer scanning with invariant tracking efficiently.
反转单词前缀
Reverse the prefix of a string starting from index 0 to the first occurrence of a given character.
给植物浇水 II
Simulate watering a row of plants with Alice and Bob using two-pointer scanning, tracking refills precisely for each ste…
找出数组中的第一个回文字符串
Return the first palindromic string from an array of words or an empty string if none exists.
向字符串添加空格
Learn to efficiently insert spaces in a string using two-pointer scanning with invariant tracking in linear time.
按符号重排数组
Rearrange an array by alternating positive and negative integers using a two-pointer approach with invariant tracking.
根据给定数字划分数组
Rearrange an array around a pivot while maintaining relative order using a two-pointer scanning approach efficiently.
得到回文串的最少操作次数
The problem challenges you to find the minimum number of adjacent swaps to make a string a palindrome.
找出数组中的所有 K 近邻下标
Identify all indices in an array that are within k distance of elements equal to the given key using two-pointer scannin…
移动片段得到字符串
Determine if the pieces in start can be moved to form target using two-pointer scanning and strict left-right movement r…
严格回文的数字
Determine if a number is strictly palindromic in all bases from 2 to n minus 2 using two-pointer scanning and invariant …
将区间分为最少组数
Determine the minimum number of non-overlapping groups for a set of intervals using precise two-pointer scanning logic.
运动员和训练师的最大匹配数
Maximize the number of valid player-trainer matchings using two-pointer scanning and careful sorting strategy.
对数组执行操作
Transform an array by applying adjacent doubling operations and shifting zeros efficiently using two-pointer scanning te…
雇佣 K 位工人的总代价
Optimize the total cost of hiring exactly k workers using a two-pointer approach with invariant tracking and priority qu…
追加字符以获得子序列
Determine the minimum characters to append to s so t becomes a subsequence using efficient two-pointer scanning techniqu…
矩阵查询可获得的最大分数
Solve the Maximum Number of Points From Grid Queries problem using two-pointer scanning and invariant tracking.
最多可以摧毁的敌人城堡数目
Find the maximum number of enemy forts that can be captured by moving your army using two-pointer scanning with invarian…
找出数组的串联值
Compute the array concatenation value efficiently using two-pointer scanning while maintaining a running total of concat…
最大化数组的伟大值
Maximize Greatness of an Array requires permuting numbers to exceed original values at most indices efficiently.
字典序最小回文串
Given a string, make it a palindrome with the fewest operations, prioritizing lexicographically smallest result.
循环增长使字符串子序列等于另一个字符串
Determine if str2 can be made a subsequence of str1 by incrementing characters cyclically using at most one operation.
找出满足差值条件的下标 I
Find two indices in an array where their difference in both index and value meets specified thresholds.
找出满足差值条件的下标 II
Locate two indices in an array where both index and value differences meet specified thresholds using precise scanning t…
区分黑球与白球
Solve the "Separate Black and White Balls" problem by swapping adjacent balls to group all black balls to the right with…
找出与数组相加的整数 II
Given two arrays nums1 and nums2, determine the integer added to nums1 to make it equal to nums2 after removing two elem…
最小元素和最大元素的最小平均值
Solve the Minimum Average of Smallest and Largest Elements problem using two-pointer scanning to track averages effectiv…
最少翻转次数使二进制矩阵回文 I
Find the minimum flips to make a binary grid palindromic by scanning with two pointers and tracking symmetry efficiently…
最少翻转次数使二进制矩阵回文 II
The problem asks to flip cells in a binary grid to make its rows and columns palindromic using the minimum number of fli…
从盒子中找出字典序最大的字符串 I
This problem involves finding the lexicographically largest string from a given word using a two-pointer approach.
子序列首尾元素的最大乘积
Maximize the product of the first and last elements of any subsequence of size m in an integer array.