Removing elements from arrays for brushing questions


foreword

insert image description here


The order of the questions in each of my brushing questions is specially arranged. The purpose is to proceed step by step, so as to thoroughly master this kind of method. I mainly share the algorithm. I will not write the solution to the violent solution. This article uses double pointers

1. Remove elements

1. Topic introduction

Corresponding to 27. Remove elements of Likou

Given an array nums and a value val, you need to remove all elements whose value is equal to val in place, and return the new length of the removed array. Don't use extra array space, you have to use only O(1) extra space and modify the input array in-place. The order of elements can be changed. You don't need to consider elements in the array beyond the new length.

insert image description here

2. Ideas:

We use the fast and slow pointers, and the fast pointer looks backward to find the number that is not equal to val (that is, the value required by the new array), and assigns it to the value where the slow pointer is located, so that after the fast traversal, we will not be equal to val All the numbers are moved to the front

3. Code

int removeElement(int* nums, int numsSize, int val){
    
    
    int slow =0;
    int fast=0;
    if(numsSize==0)
    {
    
    
        return 0;
    }
   while(fast<numsSize)
   {
    
    
       if(nums[fast]!=val)
       {
    
    
           nums[slow++]=nums[fast];
       }
           fast++;  
   }
   return slow;
}

2. Moving Zero

1. Topic introduction

Topic corresponding to stress buckle 26. Delete duplicates in an ordered array

Given an array nums, write a function to move all 0s to the end of the array while maintaining the relative order of the nonzero elements.
Note that arrays must be manipulated in-place without copying them.

2. Ideas

After laying the foundation with the idea of ​​the previous question, this question is basically easy to do. We can regard 0 as val, let fast find a value that is not equal to val, and then assign it to the value of slow, and finally set slow All future values ​​are assigned 0
insert image description here

3. Code

void moveZeroes(int* nums, int numsSize){
    
    
    int slow=0;
    int fast=0;
    while(fast<numsSize)
    {
    
    
        if(nums[fast]!=0)
        {
    
    
            nums[slow++]=nums[fast];
        }
        fast++;
    }
    for(int i=slow;i<numsSize;i++)
    {
    
    
        nums[i]=0;
    }
}

3. Remove duplicates in an ordered array

1. Topic introduction

insert image description here

2. thought

We define a slow and fast, as long as fast is not equal to slow, we can overwrite fast to slow+1, and we keep a slow

3. Code

int removeDuplicates(int* nums, int numsSize){
    
    
    int slow=0;
    int fast=0;//这里可以=1,因为第一个始终是会保留的
    while(fast<numsSize)
    {
    
    
        if(nums[fast]!=nums[slow])
        {
    
    
            nums[slow+1]=nums[fast];
            slow++;
        }
        fast++;    
    }
    return slow+1;
        
}

4. 80. Delete duplicates in an ordered array II

1. Topic introduction

This question is to test whether you have a deep understanding of the previous question. The question should be deducted 80. Delete duplicates in an ordered array II
insert image description here

2. Ideas

Although the double-pointer removal method is an algorithm for in-place operations, we remove elements to reconstruct the array we need from the array to be processed, so this question requires us to have such thoughts, so that we can understand this This question needs to be reserved so that the elements that appear more than twice appear only twice, then we need to compare the processed array with the first two of the array we need to construct, and only the difference can be assigned to slow

3. Code

int removeDuplicates(int* nums, int numsSize) {
    
    
    if (numsSize <= 2) {
    
    
        return numsSize;
    }
    int slow = 2, fast = 2;
    while (fast < numsSize) {
    
    
        if (nums[slow - 2] != nums[fast]) {
    
    
            nums[slow] = nums[fast];
            ++slow;
        }
        ++fast;
    }
    return slow;
}

4. Recommended solution

The analysis of the problem solution is more clear with my analysis
insert image description here


Guess you like

Origin blog.csdn.net/Ruiren_/article/details/129186979