Title: |
Remove duplicates from Sorted Array II |
Pass Rate: |
30.7% |
Difficulty: |
Medium |
Ollow up for "Remove duplicates":
What if duplicates is allowed at the 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]
.
Made the first version of it. is to remove the repetition, and use a pointer to record where the real should be.
Now do remove more than 2 of the repetition, still with 1, there is a difference is to use a flag bit to determine whether it is greater than 2 on the line
See the code specifically:
1 Public classSolution {2 Public intRemoveDuplicates (int[] A) {3 if(a.length==0)return0;4 intIndex=1;5 intFlag=0;6 for(inti=1;i<a.length;i++){7 if(a[i]!=a[i-1]){8a[index]=A[i];9index++;TenFlag=0; One } A Else if(a[i]==a[i-1]){ - if(flag!=2){ -a[index]=A[i]; theindex++; -flag=2; - } - } + } - returnindex; + } A}
Leetcode------Remove Duplicates from Sorted Array II