首页 > 代码库 > Remove Element

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.

思路:

 1 class Solution { 2 public: 3     int removeElement( int A[], int n, int elem ) { 4         int idx = 0; 5         for( int i = 0; i < n; ++i ) { 6             if( A[i] != elem ) { 7                 A[idx++] = A[i]; 8             } 9         }10         return idx;11     }12 };

 

Remove Element