Remove Duplicates from Sorted Array II

来源:互联网 发布:淘宝天天特价怎么找 编辑:程序博客网 时间:2024/06/07 13:46

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很雷同!

int removeDuplicates(int A[], int n) {                int current,index;        int count;        current = 1;        index = 0; //取值很关键        count = 1;                if(n <= 2 )return n;        while(current <  n)        {            if(A[current] == A[current-1])            {                count++;                if(count <= 2)                 {                    A[++index]=A[current];//主要在这个地方,小于2的重复,还是的覆盖!                }                current++;                            }            else               {                   count = 1;                A[++index]=A[current];                                current ++;            }                    }        return index+1 ;    }


//主要是俩下标,以及计数值,并且后面的小于2的重复的数,向前覆盖即可!

原创粉丝点击