问题:
Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?
For example,
Given sorted array A =[1,1,1,2,2,3],
Your function should return length =5, and A is now[1,1,2,2,3].
说明:数组从后面向前面覆盖,做过好几次了,要找满足条件的覆盖
解题:
public class Solution {
public int removeDuplicates(int[] A) {
if(A==null)
return 0;
int len=A.length;
if(len<=2)
return len;
int index=2;
for(int i=2;i<len;i++){
if(A[i]!=A[index-2])
A[index++]=A[i];
}
return index;
}
}