LeetCode 80 Remove Duplicates from Sorted Array II

来源:互联网 发布:nob2b外贸搜索软件 编辑:程序博客网 时间:2024/05/17 02:23

题目










分析



对待这种题我一般就是把需要除去的数赋予一个很大的值,最后经过一个sort,所有不需要的数就到最后面去了。





题解






class Solution {public:    int removeDuplicates(int A[], int n) {        int temp;        int length=1;        int count=0;        if(n==0)            return 0;        temp=A[0];        for(int i=1;i<n;i++){            if(A[i]==temp){                length++;                if(length>2){                    A[i]=65536;                    count++;                    length=2;                }            }            else{                length=1;                temp=A[i];            }        }        sort(A,A+n);        return n-count;    }};




0 0