摘要:移除元素是指将数组中的指定元素移除,并返回移除后的数组的大小。例如,在数组12345中,将3移除。那么,移除之后数组变成1245,返回数组大小为4。
分享兴趣,传播快乐,增长见闻,留下美好!
亲爱的您,这里是LearningYard新学苑。
今天小编为大家带来“畅游算法之海(二)——移除元素”,欢迎您的访问。
Share interest, spread happiness, increase knowledge, and leave beautiful.
Dear, this is the LearingYard Academy!
Today, Algorithms (2) - Removing elements ,welcome to visit!
一、 简介
1. Introduction
移除元素是指将数组中的指定元素移除,并返回移除后的数组的大小。例如,在数组12345中,将3移除。那么,移除之后数组变成1245,返回数组大小为4。
Removing an element removes a specified element from an array and returns the size of the array after the removal. For example, in the array 12345, remove the 3. Then, the removed array becomes 1245, returning an array size of 4.
二、 原理
2. Principle
这里的移除元素并不是简单的移除。数组中的元素在存储空间上是连续的。比如数组12345中的1和2是相邻的。那么,只是简单地将3移除后,2和4之间是有一个空位的。而我们想要的是移除3后,1245在空间上是连续的。所以,移除元素用到了一种逻辑上的删除,就是让后面的元素依次覆盖前面的元素,即4覆盖3,5覆盖4。
The removal element here is not a simple removal. The elements of an array are contiguous in storage space. For example, the 1's and 2's in array 12345 are adjacent. So, by simply removing the 3, there is a space between the 2 and the 4. Whereas what we want is that after removing 3, 1245 is spatially continuous. So, removing an element involves a logical deletion, where the following element in turn overwrites the preceding element-4 over 3 and 5 over 4.
可又有一个问题,最后的5不也在数组中吗?是的,最后的5会占一个空间,但只要返回删除后的数组大小4,那么数组就只包含从数组首地址存放的1和后面的2,4,5三个元素,最后的5就不会被访问到,实现逻辑上的删除。
But here's the problem.Isn't the last 5 in the array, too? Yes, the last 5 takes up a space, but if you return the removed array of size 4, then the array will contain only the 1 at the beginning of the array and the following 2, 4, and 5 elements, and the last 5 will not be accessed, thus logically removing it.
三、 实现
3. Implementation
所以计算机中删除元素的本质就是向前覆盖。那么,知道原理后,如何用代码实现呢?
So the essence of deleting elements in computers is forward overwriting. Now that you know how it works, how do you implement it in code?
力扣上有一道相关的题,编号72。已知数组和想要删除的元素,简单模拟一下移除元素的过程。
There is a related question on the force buckle, No. 72. Given the array and the elements you want to remove, let's briefly simulate the process of removing an element.
根据上述流程,移除元素就需要用到两层循环,第一层遍历数组中的元素,并根据判断结果执行相应操作,第二层循环就是依次向前覆盖的操作。
This process requires two loops to remove an item, one to iterate over the items in the array and perform an action based on the result, and the second loop to override one by one.
根据流程,在力扣上编写代码,实现移除元素操作。其中,代码有两个关键点,一是每进行一次覆盖操作,就要更新数组的大小,二是进行覆盖操作后,保证i不变,要继续判断当前下标的元素。
According to the process, write code on the button to remove the element. There are two key points in the code: one is that the size of the array needs to be updated every time the overwrite operation is performed, and the other is that after the overwrite operation, the current index needs to be evaluated while keeping the i index unchanged.
最后运行通过,成功移除元素。
Finally, it passes, removing the element successfully.
四、 双指针
4. Double Pointer
上面是普通的方法,通过两层循环实现,逻辑上的删除。而接下来的方法仅用一层循环就能实现。为何能做到这点呢?就好比,一个人要做一件事很困难,但两个人分工合作就能更高效地完成任务,这就是双指针。
The above is the normal method, implemented by a two-level loop, logical deletion. The following method can be implemented with a single loop. Why is this possible? For example, it is difficult for one person to do one thing, but two people can complete the task more efficiently by dividing the labor, which is called double pointer.
双指针是数组的两个下标变量。快指针为fast,遍历数组中的整个元素,向慢指针发送信号。慢指针为slow,接收信号,并更新数组。双指针的核心在于快指针从数组中提取出新数组中的各个元素传给慢指针,且慢指针对数组的操作不会影响到快指针的遍历。
A double pointer is two index variables of an array. The fast pointer, called fast, iterates over the entire element of the array and sends a signal to the slow pointer. The slow pointer is slow, which receives the signal and updates the array. The core of dual pointer is that the fast pointer extracts each element of a new array from the array and passes it to the slow pointer, and the operation of the slow pointer on the array does not affect the traversal of the fast pointer.
双指针和上个方法都是通过覆盖,实现删除。但双指针独特的覆盖模式却比一次又一次的向前覆盖要高效得多。接下来就简单模拟一下双指针删除元素的过程。
Both the double pointer method and the previous method perform deletion by overwriting. However, the unique coverage pattern of dual Pointers is much more efficient than overwriting forward again and again. Let's simulate a simple double-pointer deletion.
快指针遍历结束后,slow的值必然是数组的大小。所以直接返回slow即可,节省空间。
At the end of the fast pointer traversal, the value of slow must be the size of the array. So just return slow to save space.
只要过程清楚了,代码还不是轻轻松松,同样可以实现删除。
As long as the process is clear, the code is not easy to delete.
今天的分享就到这里了,
如果您对文章有独特的想法,
欢迎给我们留言。
让我们相约明天,
祝您今天过得开心快乐!
That's all for today's sharing.
If you have a unique idea about the article,
please leave us a message,
and let us meet tomorrow.
I wish you a nice day!
翻译:网易有道翻译
本文由LearningYard学苑整理并发出,如有侵权请后台留言沟通.
文案|Dongyang
排版|Dongyang
审核|hong
Learning Yard 新学苑
来源:LearningYard学苑