Remove Element
Given an array and a value, remove all occurrences of that value in place and return the new length.
The order of elements can be changed, and the elements after the new length don't matter.Have you met this question in a real interview? Yes
Example
Given an array [0,4,4,0,0,2,4,4]
, value=4
return 4
and front four elements of the array is [0,0,0,2]
这道题让我们移除一个数组中和给定值相同的数字,并返回新的数组的长度。是一道比较容易的题,我们只需要一个变量用来计数,然后遍历原数组,如果当前的值和给定值不同,我们就把当前值覆盖计数变量的位置,并将计数变量加1。
Last updated