remove-duplicates-from-sorted-array-ii

/ ** 
* for the Follow up "the Remove Duplicates":
* Duplicates are allowed the What IF AT MOST Twice?
* The For Example,
* the Given sorted Array A = [1,1,1,2,2,3],
* Your function Should length = 5 return, and now iS a [1,1,2,2,3].
*
* "remove duplicates" next:
* If you allow up to twice repeat it?
* For example,
* = given sorted array a [1,1,1,2,2,3],
* function returns the length should be 5, is now a [1,1,2,2,3].
* /

Because this problem is to allow the number repeated twice, so we will count = 2, is circulated.
/ ** 
 * for the Follow up "the Remove Duplicates": 
 * Duplicates are allowed the What IF AT MOST Twice? 
 * The For Example, 
 * the Given sorted Array A = [1,1,1,2,2,3], 
 * Your function Should length = 5 return, and now iS a [1,1,2,2,3]. 
 * 
 * "remove duplicates" next: 
 * If you allow up to twice repeat it? 
 * For example, 
 * = given sorted array a [1,1,1,2,2,3], 
 * function returns the length should be 5, is now a [1,1,2,2,3]. 
 * / 

Public class Main48 { 
    public static void main (String [] args) { 
        int [] = {1,1,1,2} A; 
        System.out.println (Main48.removeDuplicates (A)); 
    } 

    public static int RemoveDuplicates (int [] A) { 
        IF (A == null) {  
            return 0;
        } 
        IF (A.length <= 2) { 
            return A.length; 
        } 
        int COUNT = 2; 
        for (int I = 2; I <A.length; I ++) {// if the same thing is skipped.
       if (A [i]! = A [count-2]) {// Once the second number is not the same in front with it added the equivalent array, covering the same number of positions of the previous cycle. 
                       A [COUNT ++] = A [I]; } } return COUNT; } }

  

Guess you like

Origin www.cnblogs.com/strive-19970713/p/11344641.html