zoukankan      html  css  js  c++  java
  • Remove Duplicates from Sorted Array II 分类: Leetcode(线性表) 2015-03-25 21:20 40人阅读 评论(0) 收藏

    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) {
           if(n==0) return 0;
           int idx = 0, count = 0 , i = 0;
           for ( i = 0; i < n ; i++) {
               if (i >0 && a[i] ==a[i-1]) {
                   count++;
                   if(count >= 3){
                       continue;
                   }
               }
               else {
                   count = 1;
               }
               a[idx++] = a[i];
           }
           return idx;
        }
    };

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    CGCDSSQ
    100200H
    斗地主
    借教室
    bzoj 3743
    17B
    能量项链
    589
    16-求连续数组和最大
    15-幸运数组4、7
  • 原文地址:https://www.cnblogs.com/learnordie/p/4656946.html
Copyright © 2011-2022 走看看