LeetCode—Remove Elements

foreword

Question: 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. topic link

There are the following solutions, some of which do not meet the requirements of the topic will be marked, and the writing is just to provide an idea. Please correct me if I am wrong!

1. Violent solution

Iterates through the entire array, and whenever an element equal to val is encountered, it is removed from the array. Until the entire array is traversed, the new array length is returned.
insert image description here
Code:

//暴力求解
int removeElement(int* nums, int numsSize, int val) {
    
    
	int i = 0;
	while (i < numsSize)
	{
    
    
		if (nums[i] == val)
		{
    
    
			for (int j = i; j < numsSize - 1; j++) //把后面的元素往前移动进行覆盖,就相当于删除val元素
			{
    
    
				nums[j] = nums[j + 1];
			}
		}
		else
			i++;
	}
	return numsSize; //返回新数组的长度
}

Time complexity is O ( n 2 ) O(n^2)O ( n2 ), the space complexity isO ( 1 ) O(1)O ( 1 ) .
It meets the requirements of the topic, but the efficiency is too low.

2. Create a temporary space

Create a temporary array, copy the elements not equal to val in the original array to the temporary array, and finally copy the temporary array back to the original array.
insert image description here

Code:

//创建临时空间
int removeElement(int* nums, int numsSize, int val) {
    
    
	int* tmp = (int*)malloc(sizeof(int) * numsSize);
	if (tmp == NULL) //这个判断其实也可以省略,在本题中。
	{
    
    
		perror("malloc fail\n");
		return;
	}
	int count = 0;
	int i = 0;
	for (i = 0; i < numsSize; i++)
	{
    
    
		if (nums[i] != val)
		{
    
    
			tmp[count] = nums[i];
			count++;
		}
	}
	for (i = 0; i < count; i++)
	{
    
    
		nums[i] = tmp[i]; 
	}
	free(tmp);//释放临时空间,这题中可以不用把tmp置为NULL
	return count;
}

After releasing the memory of the temporary array in this question, there is no need to use the tmp pointer, nor to set it to NULL. Because at the end of the function, this pointer will be destroyed and no longer exist, so there will be no dangling pointer problem.
Time complexity is O ( n ) O(n)O ( n ) , the space complexity isO ( n ) O(n)O ( n ) .
Compared with the first type, although the efficiency is improved, the space complexity does not meet the requirements of the topic.

3. Double pointer method

The basic idea of ​​the double pointer method is to maintain a slow pointer and a fast pointer. The fast pointer is used to traverse the entire array, and the slow pointer is used to point to the position of the element that needs to be preserved. When the fast pointer encounters an element that needs to be deleted, the fast pointer continues to traverse forward, and the slow pointer remains unchanged until it encounters an element that needs to be retained, it is copied to the position of the slow pointer and the slow pointer is moved forward one bit .
insert image description here
Code:

int removeElement(int* nums, int numsSize, int val){
    
    
    int src=0;
    int dest=0;
    while(src<numsSize)
    {
    
    
        if(nums[src]!=val)
        {
    
    
            nums[dest++]=nums[src++];
        }
        else
        {
    
    
            src++;
        }
    }
    return dest; //返回长度
}

Time complexity is O ( n ) O(n)O ( n ) , the space complexity isO ( 1 ) O(1)O ( 1 ) .
It can be seen that the best solution is the double pointer method, which is not only time efficient, but also saves space!

That's all for today's algorithm sharing!

insert image description here

Guess you like

Origin blog.csdn.net/qq_69218005/article/details/130180919