Remove Duplicates from Sorted Array II

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差不多,只是说多了一个限制条件

 1 public class Solution {
 2     public int removeDuplicates(int[] A) {
 3         if(0 == A.length)
 4             return 0;
 5         
 6         int B[] = new int[A.length];
 7         B[0] = A[0];
 8         boolean isSecond = false;
 9         int k = 1;
10         for(int i = 1; i < A.length; i++){
11             if(A[i] != B[k - 1]){
12                 B[k++] = A[i];
13                 isSecond = false;
14             }//if
15             else if(A[i] == B[k - 1] && isSecond == false){
16                 B[k++] = A[i];
17                 isSecond = true;
18             }//else if
19         }//for
20         
21         System.arraycopy(B, 0, A, 0, k);
22         
23         return k;
24     }
25 }
原文地址:https://www.cnblogs.com/luckygxf/p/4213433.html