写双指针的时候,一定要注意变化左右指针,容易在关注逻辑的时候忘了变化指针
在数组上应用双指针:
类型一:左指针当作数组下标,右指针当作需要给当前下标赋值的值
- leetcode26 Remove Duplicates from Sorted Array
- leetcode27 Remove Element
- leetcode283 Move Zeroes
以上这些题都是把左指针当作数组下标,右指针当作需要给当前下标赋值的值
27题示例代码。left是数组的index,right是value
int left = 0;
for(int right = 0; right < nums.length; right++) {
if(nums[right] != val) {
nums[left] = nums[right];
left++;
}
}
return left;
类型二:双向双指针
3sum,永远的痛,做了还忘的经典
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
Example 2:
Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.
Example 3:
Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.
不太适合用哈希,因为还要去重
拿这个nums数组来举例,首先将数组排序,然后有一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。
依然还是在数组中找到 abc 使得a + b +c =0,我们这里相当于 a = nums[i],b = nums[left],c = nums[right]。
接下来如何移动left 和right呢, 如果nums[i] + nums[left] + nums[right] > 0 就说明 此时三数之和大了,因为数组是排序后了,所以right下标就应该向左移动,这样才能让三数之和小一些。
如果 nums[i] + nums[left] + nums[right] < 0 说明 此时 三数之和小了,left 就向右移动,才能让三数之和大一些,直到left与right相遇为止。
时间复杂度:O(n^2)。
接下来是去重,需要对a, b, c分别进行去重
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0; i < nums.length - 2; i++) {
if(nums[i] > 0) return res;
// 对a进行去重
if(i > 0 && nums[i] == nums[i-1]) continue;
int left = i+1;
int right = nums.length - 1;
while(left < right) {
if(nums[i] + nums[left] + nums[right] > 0) {
right--;
} else if (nums[i] + nums[left] + nums[right] < 0) {
left++;
} else {
res.add(Arrays.asList(nums[i], nums[left], nums[right]));
// 分别对left和right进行去重
while(right > left && nums[right] == nums[right - 1]){
right--;
}
while(right > left && nums[left] == nums[left + 1]){
left++;
}
right--;
left++;
}
}
}
return res;
}
}
四数之和,和三数之和是一个思路,都是使用双指针法, 基本解法就是在三数之和 的基础上再套一层for循环。需要做两层剪枝处理。剪枝处理的时候要注意当前nums[i]要大于等于0。否则如果target是负数,遇到nums[i] > target就直接返回了
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int i = 0; i < nums.length; i++) {
// 此处必须nums[i] >= 0, 如果是[-4, -3, -2, -1], target是-10,就直接返回了
if (nums[i] > target && nums[i] >= 0){
break;
};
if (i > 0 && nums[i] == nums[i-1]) continue;
for(int j = i + 1; j < nums.length; j++) {
// 同理
if(nums[i] + nums[j] > target && nums[i] + nums[j] >= 0){
break;
} ;
if(j > i+1 && nums[j] == nums[j-1]) continue;
int left = j + 1;
int right = nums.length - 1;
while(left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if(sum < target) {
left++;
} else if(sum > target) {
right--;
} else {
res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
while(left < right && nums[right] == nums[right-1]) right--;
while(left < right && nums[left] == nums[left+1]) left++;
left++;
right--;
}
}
}
}
return res;
}
}
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example 2:
Input: height = [1,1]
Output: 1
这道题第一次做也做不出来,根本就想不到要用双指针
left和right分别指向最左边和最右边。然后呢判断是左边的height高还是右边的height高,如果是哪边矮就移动哪边。因为移动矮的可能找到更好的结果,移动高的会变得更差。
class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int res = 0;
while(left < right) {
res = Math.max(res, Math.min(height[left], height[right]) * (right - left));
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return res;
}
}