Remove Element

class Solution {
public:
    int removeElement(int A[], int n, int elem) {
        
        int cnt = 0;
        for(int i=0;i<n;i++)
        {
            if(A[i] != elem)
            {
                A[cnt++] = A[i];
            }
        }
        return cnt;
    }
};

  使用一个cnt记录不相同元素的,当遇到相同元素就跳过cnt不记录,最后就会将所有不同于elem的元素记录下来了

posted @ 2015-04-29 10:10  sunalive  Views(95)  Comments(0)    收藏  举报