Rabbit series brush questions leetcode series two double pointer

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.

The sum of one and two numbers

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-Fq1VDtbM-1585623546685)(D3F6878EF1AA48D4B39E15260DF95578)]

(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

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-b28zGUXX-1585623546686)(8CD814F70A794BD99FAC9F429A3C20A7)]

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

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-ZtNXSMEK-1585623546688)(1171EA4376B84B058C051D6E9A09DE3E)]

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;
    }
}

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-xA3avRPP-1585623546690)(D0A33C3A950345BBB152AC5EC89B1A80)]

Topic four catch rain

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-tcq3sgyw-1585623546693)(58DD1BF4F2B54F378F362FCA252B8ED9)] [External link image transfer failure, the origin site may have an anti-leech link mechanism, It is recommended to save the picture and upload it directly (img-dHs8nXM2-1585623546693) (46004E6682E845AC8796789C6133D90F))

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;
    }
}

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-zZgeuN7v-1585623546697) (D2975AD108B64993907C97E3E5E2BD30)]

Guess you like

Origin blog.csdn.net/hch977/article/details/105217786