leetcode 39: 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) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int i=0, j=n-1;
        
        while( i<=j) {
            if( A[i] == elem) {
                while( j>=i && A[j] == elem ) j--;
                if( j<= i ) break;
                A[i] = A[j--];
            }
            i++;
        }
        
        return i;
    }
};
 
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号