[LeetCode] 084: Remove Element

来源:互联网 发布:点餐系统数据库设计 编辑:程序博客网 时间:2024/06/08 10:32
[Problem]

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.


[Solution]
class Solution {
public:
int removeElement(int A[], int n, int elem) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int i = 0, j = 0;
while(j < n){
if(A[j] != elem){
A[i++] = A[j];
}
j++;
}
return i;
}
};
说明:版权所有,转载请注明出处。Coder007的博客
原创粉丝点击