Double pointer
As the name implies, two pointers have
done a lot of questions. Generally, there are two modes.
One is i and j pointers from the beginning to the middle respectively. The other
is i and j pointers traverse the two parts respectively.
Article Directory
The sum of one and two numbers
(In order to explain the double pointer, this topic is changed to return [2,7].
Of course, the original topic can be faster with a hash table)
Ideas:
After sorting, double pointers are used, so that the code time complexity is reduced to O(nlogn). The
i pointer starts from 0 and the
j pointer starts from the last bit.
If the number on the i pointer plus the number on the j pointer is greater than k, then j—,
Otherwise i+
Code:
public static int[] twoSum(int[] nums, int target) {
Arrays.sort(nums);
int i=0;
int j=nums.length-1;
while(i<j)
{
if(nums[i]+nums[j]==target)
{
return new int[] {
nums[i],nums[j]};
}
else if(nums[i]+nums[j]>target)
{
j--;
}
else if(nums[i]+nums[j]<target)
{
i++;
}
}
return new int[] {
};
}
The sum of two and three numbers
Ideas
Do the problem on the basis of the sum of two numbers. Using double pointers can reduce the complexity to O(n2).
Sort
t pointers to the last negative number.
i pointers start from t+1 and
j pointers start from the last pointer.
Code
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int t=0;
List<List<Integer>> a=new ArrayList<>();
if(nums.length==0)
return a;
Arrays.sort(nums);
int q=nums[0];
while(t<nums.length&&nums[t]<0)
{
if(t!=0&&nums[t]==q)
{
t++;
continue;
}
int i=t+1;
int j=nums.length-1;
while(i<j)
{
if(nums[i]+nums[j]==-nums[t])
{
List<Integer> bIntegers=new ArrayList<>();
bIntegers.add(nums[t]);
bIntegers.add(nums[i]);
bIntegers.add(nums[j]);
a.add(bIntegers);
i++;
while(nums[i]==nums[i-1]) i++;
j--;
while(nums[j]==nums[j+1]) j--;
}
else if(nums[i]+nums[j]<-nums[t])
{
i++;
while(nums[i]==nums[i-1]) i++;
}
else {
j--;
while(nums[j]==nums[j+1]) j--;
}
}
q=nums[t];
t++;
}
return a;
}
}
# Topic three squares of ordered array
Ideas:
Using double pointers i and j, starting from the two ends of the array respectively, i traverses the negative part, j traverses the positive part, and combines the double pointers to synthesize the desired array.
Code:
class Solution {
public int[] sortedSquares(int[] A) {
int[] nums=new int[A.length];
int i=0;
int j=A.length-1;
int q=nums.length-1;
while(i<=j)
{
int a=(int) Math.pow(A[i], 2);
int b=(int) Math.pow(A[j], 2);
if(a>=b)
{
nums[q]=a;
i++;
}
else {
nums[q]=b;
j--;
}
q--;
}
return nums;
}
}
Topic four catch rain
Ideas:
This question is great!
I have been thinking for a long time.
I think that to understand this question, we
must first understand its essence,
and then understand how to realize the calculation by combining the two pointers with the actual calculation form.
substance:
What determines the amount of stored rain?
If the wall height of the point -min (the highest wall on the left and the highest wall on the right)
is positive, it represents the amount of rain stored at this point
Double pointer:
We set two pointers i and j
to move from both sides of the array to the middle.
When we can determine the storage rainfall of one point, the pointer will move to the next position.
When can the stored rainfall amount be determined?
When our pointer is traversing, we will access maxleft, maxright
maxleft must be correct for point i, but naxright is not necessarily correct (because the traversal is not completed, maxright is only for maxright at point j).
Review the essence of storing rainfall again.
If maxleft<maxright for point
i, the rainfall at point i can be determined by maxleft.
If for point j, maxright<maxleft, it means that
the rainfall at point j can be determined by maxright.
Code:
class Solution {
public int trap(int[] height) {
int sum=0;
int maxleft=0;
int maxright=0;
int i=0;
int j=height.length-1;
while(i<=j)
{
if(maxleft<=maxright)
{
if(maxleft-height[i]>0)
{
sum=sum+maxleft-height[i];
}
if(height[i]>maxleft)
{
maxleft=height[i];
}
i++;
}
else {
if(maxright-height[j]>0)
{
sum=sum+maxright-height[j];
}
if(height[j]>maxright)
{
maxright=height[j];
}
j--;
}
}
return sum;
}
}