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]
.
给定一个有序数组,给数组去重,和Remove Duplicates from Sorted Array不同
class Solution { public: int removeDuplicates(int A[], int n) { if(n<=2)return n; int p1=1; int counter=1; for(int i=1; i<n; i++){ if(A[i]==A[i-1]){ counter++; if(counter<=2){A[p1]=A[i]; p1++;} } else{ counter=1; A[p1]=A[i]; p1++; } } return p1; } };
LeetCode: Remove Duplicates from Sorted Array II [080],布布扣,bubuko.com
LeetCode: Remove Duplicates from Sorted Array II [080]
原文地址:http://blog.csdn.net/harryhuang1990/article/details/27533929