LeetCode 27. Remove Element
Given an array and a value, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
解析:
给定一个数组和一个目标数字,要求从数组中删除给定的数字,并返回删除之后的长度。题目要求采用原地算法,空间复杂度为O(1)。
首先来看一下原地算法(in-place)的解释(来自维基百科):
在计算机科学中,一个原地算法(in-place algorithm)基本上是一种不需要附加数据结构,然而,附加变量允许有小的额外空间来转换数据的算法。当算法运行时,输入的数据通常会被要输出的部分覆盖掉。不是原地算法有时候称为非原地(not-in-place)或不得其所(out-of-place)。
一个算法有时候会错误地被称为原地算法,只因为它用它的输出数据会覆盖掉它的输入数据。事实上这条件既不充分(在快速排序案例中所展示的)也非必要;输出数据的空间可能是固定的,或如果以输出为流数据而言,也甚至是可能无法被数清楚的。另一方面来看,有时候要决定一个算法是不是原地,而数它的输出空间可能是比较可行的,像是底下的第一个的reverse示例;如此使得它更难去严格地定义原地算法。在理论上的应用像是log-space reduction,更是典型的总是忽略输出的空间(在这些状况,更重要的是输出为仅能写入)。
思路1:
题目限制了空间复杂度,并且采用原地算法。可以采用两个指针,一个记录当前遍历的数组位置,另一个记录当前出去目标元素的数组长度。
[cc lang=”C++”]
class Solution {
public:
int removeElement(vector
int i = 0;
int j = 0;
for(i = 0; i < nums.size(); ++i){
if(nums[i] == val) continue;// 只拷贝非给定数字的元素
nums[j] = nums[i];
j++;
}
return j;
}
};
[/cc]
思路2
遍历数组过程中,如果当前元素是目标元素,则与当前数组的末尾交换,这样能够减少赋值的次数。
[cc lang="C++"]
class Solution {
public:
int removeElement(vector
int size = nums.size();
int i=0;
while(i
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
};
[/cc]