Leetcode: 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].

第二遍做法:

 1 public class Solution {
 2     public int removeDuplicates(int[] A) {
 3         if (A == null || A.length == 0) return 0;
 4         int len = 1;
 5         int count = 1;
 6         for (int i=1; i<A.length; i++) {
 7             if (A[i] == A[len-1] && count < 2) {
 8                 A[len] = A[i];
 9                 len++;
10                 count++;
11             } 
12             else if (A[i] != A[len-1]) {
13                 A[len] = A[i];
14                 len++;
15                 count = 1;
16             }
17         }
18         A = Arrays.copyOf(A, len);
19         return len;
20     }
21 }
原文地址:https://www.cnblogs.com/EdwardLiu/p/3740991.html