Remove Duplicates from Sorted Array II 允许重复2次数组

来源:互联网 发布:vb程序设计免费版下载 编辑:程序博客网 时间:2024/04/28 02:42

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

class Solution {public:    int removeDuplicates(int A[], int n) {                int i,f=1,k=0;        for(i=0;i<n;i++)        {            if(A[i]==A[i-1]&&i>0)                f++;            else                f=1;            if(f<=2)            {                A[k++]=A[i];            }        }        return k;    }};

0 0
原创粉丝点击