Remove Element

 

void swap(int* a,int* b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}
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;
        int j = n;
        while(i<j)
        {
            if(A[i]==elem)
            {
                j--;
                swap(&A[i],&A[j]);
            }else
                i++;
        }
        return j;
    }
};

  

posted @ 2013-10-04 22:53  summer_zhou  阅读(144)  评论(0编辑  收藏  举报