zoukankan      html  css  js  c++  java
  • 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].

    这道题和Remove Duplicates from Sorted Array差不多,只是说多了一个限制条件

     1 public class Solution {
     2     public int removeDuplicates(int[] A) {
     3         if(0 == A.length)
     4             return 0;
     5         
     6         int B[] = new int[A.length];
     7         B[0] = A[0];
     8         boolean isSecond = false;
     9         int k = 1;
    10         for(int i = 1; i < A.length; i++){
    11             if(A[i] != B[k - 1]){
    12                 B[k++] = A[i];
    13                 isSecond = false;
    14             }//if
    15             else if(A[i] == B[k - 1] && isSecond == false){
    16                 B[k++] = A[i];
    17                 isSecond = true;
    18             }//else if
    19         }//for
    20         
    21         System.arraycopy(B, 0, A, 0, k);
    22         
    23         return k;
    24     }
    25 }
  • 相关阅读:
    js函数调用模式
    js闭包和回调
    js原型
    oracle sql优化笔记
    shell脚本学习
    程序与资源管理
    vi/vim学习
    压缩和解压缩
    用户及用户组
    万用字符和特殊字符
  • 原文地址:https://www.cnblogs.com/luckygxf/p/4213433.html
Copyright © 2011-2022 走看看