LeetCode 80 Remove Duplicates from Sorted Array II

来源:互联网 发布:2016年淘宝卖家数量 编辑:程序博客网 时间:2024/05/19 03:20

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 || A.length<1) return 0;        int i=1;        int elem=A[0];        int counter=1;        for(int j=1; j<A.length; j++) {            if(A[j]!=elem) {                elem=A[j];                A[i++] = A[j];                counter=1;            }else{            counter++;            if(counter==2) A[i++]=A[j];            }        }        return i;          }}


0 0
原创粉丝点击