标签:
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 };
标签:
原文地址:http://www.cnblogs.com/jawiezhu/p/4446759.html