[LeetCode]Remove Duplicates from Sorted Array II

来源:互联网 发布:java报表技术 编辑:程序博客网 时间:2024/04/29 22:26
class Solution {//get the condition of copy or not copypublic:int removeDuplicates(int A[], int n) {// Start typing your C/C++ solution below// DO NOT write int main() functionint cur = 2;for (int i = cur; i < n; ++i){if( !(A[i] == A[cur-1] && A[i] == A[cur-2]) )A[cur++] = A[i];}return cur<n?cur:n;}};

second time

class Solution {public:    int removeDuplicates(int A[], int n) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        if(n == 0) return n;        int prevNum = A[0];        int prevCnt = 1;        int curIdx = 1;        for(int i = 1; i < n; ++i)        {            if(A[i] == prevNum)            {                if(prevCnt == 2) continue;                else A[curIdx++] = A[i], prevCnt++;            }            else            {                A[curIdx++] = A[i];                prevNum = A[i];                prevCnt = 1;            }        }        return curIdx;    }};


原创粉丝点击