題目
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
解題思路
給一個數組和一個數字,移除該數字在數組中所有出現的地方。
這是一個非常簡單的題目,應該不用多說,讀懂題目,一次AC。
代碼實現
class Solution {
public:
int removeElement(int A[], int n, int elem) {
if(A==NULL || n<=0) return 0;
int start = 0;
for(int i=0; i<n; ++i){
if(A[i] != elem){
A[start++] = A[i];
}
}
return start;
}
};
如果你覺得本篇對你有收穫,請幫頂。