双指针(Two Pointers)一直是程序员面试中的一个必须准备的主题, 面试中双指针出现的次数比较多,主要由于在工作中指针经常用到,指针问题能够直接反应面试者的基础知识、代码能力和思维逻辑,因此双指针的问题必须掌握。
解决双指针问题三种常用思想:
LeetCode中关于双指针的题目有以下三种类型题:
(一)双指针之左右指针相关题目:
(二)双指针之快慢指针相关题目:
(三)双指针之后序指针相关题目:
167. Two Sum II - Input array is sorted
class Solution {
public:
vector twoSum(vector& numbers, int target) {
vector res(2, -1);
int left = 0, right = numbers.size() - 1;
while(left < right){
int temp = numbers[left] + numbers[right];
if(temp > target){
right--;
}else if(temp < target){
left++;
}else{
res[0] = left + 1;
res[1] = right + 1;
return res;
}
}
return res;
}
};
15. 3Sum
nums
of n integers, are there elements a, b, c in nums
such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not contain duplicate triplets.class Solution {
public:
vector> threeSum(vector& nums) {
vector> res;
int n = nums.size();
if(n <= 2) return res;
sort(nums.begin(), nums.end());
for(int i = 0; i < n-2; i++){
int left = i + 1, right = n - 1;
while(left < right){
int temp = nums[left] + nums[right];
if(temp > -nums[i]){
right--;
}else if(temp < -nums[i]){
left++;
}else{
vector tmp{nums[i], nums[left], nums[right]};
res.push_back(tmp);
left++;
right--;
while(left < right && nums[left] == nums[left - 1]) left++;
while(left < right && nums[right] == nums[right + 1]) right--;
}
}
while(i + 1 < n -2 && nums[i] == nums[i + 1]) i++;
}
return res;
}
};
16. 3Sum Closest
nums
of n integers and an integer target
, find three integers in nums
such that the sum is closest to target
. Return the sum of the three integers. You may assume that each input would have exactly one solution.class Solution {
public:
int threeSumClosest(vector& nums, int target) {
int n = nums.size(), res = INT_MIN, small = INT_MAX;
sort(nums.begin(), nums.end());
for(int i = 0; i < n-2; i++){
int left = i + 1, right = n - 1;
while(left < right){
int temp = nums[left] + nums[right] + nums[i];
if(abs(temp - target) < small){
res = temp;
small = abs(temp - target);
}
if(temp > target){
right--;
}else if(temp < target){
left++;
}else{
return target;
}
}
while(i + 1 < n -2 && nums[i] == nums[i + 1]) i++;
}
return res;
}
};
18. 4Sum
nums
of n integers and an integer target
, are there elements a, b, c, and d in nums
such that a + b + c + d = target
? Find all unique quadruplets in the array which gives the sum of target
.target
? 找到数组中所有的四元组。注意:解决方案中不得包含重复的四元组。class Solution {
public:
vector> fourSum(vector& nums, int target) {
vector> res;
int n = nums.size();
if(n <= 3) return res;
sort(nums.begin(), nums.end());
for(int k = 0; k < n-3; k++){
for(int i = k + 1; i < n-2; i++){
int left = i + 1, right = n - 1;
int ret = target - nums[k] - nums[i];
while(left < right){
int temp = nums[left] + nums[right];
if(temp > ret){
right--;
}else if(temp < ret){
left++;
}else{
vector tmp{nums[k], nums[i], nums[left], nums[right]};
res.push_back(tmp);
left++;
right--;
while(left < right && nums[left] == nums[left - 1]) left++;
while(left < right && nums[right] == nums[right + 1]) right--;
}
}
while(i + 1 < n -2 && nums[i] == nums[i + 1]) i++;
}
while(k + 1 < n -3 && nums[k] == nums[k + 1]) k++;
}
return res;
}
};
11. Container With Most Water
class Solution {
public:
int maxArea(vector& height) {
int left = 0, right = height.size() - 1;
int res = 0;
while(left < right){
int temp = min(height[right], height[left]);
res = max(res, temp*(right - left));
if(height[right] < height[left]) right--;
else left++;
}
return res;
}
};
42. Trapping Rain Water
class Solution {
public:
int trap(vector& height) {
int res = 0, left = 0, right = height.size() - 1;
int maxleft = 0, maxright = 0;
while(left < right){
if(height[left] < height[right]){
if(height[left] > maxleft){
maxleft = height[left];
}else{
res += maxleft - height[left];
}
left++;
}else{
if(height[right] > maxright){
maxright = height[right];
}else{
res += maxright - height[right];
}
right--;
}
}
return res;
}
};
27. Remove Element
class Solution {
public:
int removeElement(vector& nums, int val) {
int slow = 0, fast = 0, n = nums.size();
while(fast < n){
if(nums[fast] != val) nums[slow++] = nums[fast];
fast++;
}
return slow;
}
};
283. Move Zeroes
nums
, write a function to move all 0
's to the end of it while maintaining the relative order of the non-zero elements.class Solution {
public:
void moveZeroes(vector& nums) {
int slow = 0, fast = 0, n = nums.size();
while(fast < n){
if(nums[fast] != 0) swap(nums[slow++], nums[fast]);
fast++;
}
}
};
26. Remove Duplicates from Sorted Array
class Solution {
public:
int removeDuplicates(vector& nums) {
int slow = 1, fast = 1, n = nums.size();
if(n <= 1) return n;
while(fast < n){
if(nums[fast] != nums[slow - 1]) nums[slow++] = nums[fast];
fast++;
}
return slow;
}
};
80. Remove Duplicates from Sorted Array II
class Solution {
public:
int removeDuplicates(vector& nums) {
int slow = 2, fast = 2, n = nums.size();
if(n <= 2) return n;
while(fast < n){
if(nums[fast] != nums[slow - 2]) nums[slow++] = nums[fast];
fast++;
}
return slow;
}
};
287. Find the Duplicate Number
class Solution {
public:
int findDuplicate(vector& nums) {
int slow = nums[0], fast = nums[nums[0]];
while(slow != fast){
slow = nums[slow];
fast = nums[nums[fast]];
}
fast = 0;
while(slow != fast){
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
};
88. Merge Sorted Array
class Solution {
public:
void merge(vector& nums1, int m, vector& nums2, int n) {
int i = m - 1, j = n - 1, k = m + n -1;
while(i >= 0 && j >= 0){
if(nums1[i] > nums2[j]) nums1[k--] = nums1[i--];
else nums1[k--] = nums2[j--];
}
while(j >= 0) nums1[k--] = nums2[j--];
}
};
如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来~