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.

Solution: Refactor: Update solution. Use two pointers.

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

 

posted @ 2014-04-05 00:37  beehard  阅读(104)  评论(0编辑  收藏  举报