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     {
 5         int i;
 6         int cnt;
 7         cnt=0;
 8         for(i=0;i<n;i++)
 9         {
10             if(a[i]==elem)
11             {
12                 cnt++;
13             }else if(cnt>0)
14             {
15                 a[i-cnt]=a[i];
16             }
17 
18         }
19 
20         return n-cnt;
21       }
22 };

 

posted on 2015-04-22 10:53  黄瓜小肥皂  阅读(138)  评论(0编辑  收藏  举报