remove-element

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.

class Solution {
public:
    int removeElement(int A[], int n, int elem) {
          if(n <=0) return 0;
        int start = 0;
        int end = n-1;
        while(start <= end){
            if(A[start] == elem){
                int tmp;
                A[start] = tmp;
                A[start] = A[end];
                A[end] = tmp;  
                --end;
            }else{
                ++start;
            } 
        }
        
        return end+1;
    }
};

 

posted on 2017-03-08 08:32  123_123  阅读(104)  评论(0编辑  收藏  举报