Follow up for "Remove Duplicates":
What if duplicates
are allowed at most twice?
1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 int len = A.length; 4 if(len<=2) return len; 5 int p1 = 1; 6 int p2 =1; 7 int count =1; 8 while(p2<len){ 9 if(A[p2]==A[p2-1]){ 10 if(count>=2){ 11 p2++; 12 continue; 13 } 14 else{ 15 count++; 16 } 17 } 18 else{ 19 count=1; 20 } 21 A[p1] = A[p2]; 22 p1++; 23 p2++; 24 } 25 return p1; 26 } 27 }
Remove Duplicates from Sorted Array II
原文:http://www.cnblogs.com/krunning/p/3547424.html