給定一個數組和一個值,在原地刪除與值相同的數字,返回新數組的長度。
元素的順序可以改變,並且對新的數組不會有影響。
樣例
給出一個數組 [0,4,4,0,0,2,4,4],和值 4
返回 4 並且4個元素的新數組爲[0,0,0,2]
public class Solution {
/**
*@param A: A list of integers
*@param elem: An integer
*@return: The new length after remove
*/
public int removeElement(int[] A, int elem) {
int length = A.length;
if(null==A||A.length<=0){
return 0;
}
for(int i=0;i<length;i++){
if(A[i]==elem){
for(int j=i;j<length-1;j++){
A[j] = A[j+1];
}
length--;
i--;
}
}
return length;
}
}