Remove Duplicates from Sorted Array II

来源:互联网 发布:听力软件 编辑:程序博客网 时间:2024/05/19 11:44
public class Solution {    public int removeDuplicates(int[] A) {        int count = 0;        int tmp = 0;        if(A == null || A.length == 0){            return 0;        }        if(A.length == 1){            return 1;        }        int pivot = A[0] - 1;        int compare = A[0];        for(int i = 1 ; i < A.length ; i++){            if(A[i] == compare){                if(tmp > 0){                    A[i] = pivot;                    count++;                }                tmp++;            }else{                compare = A[i];                //A[i - count] =A[i];                tmp = 0;            }        }        int index = 0;              for(int i = 0 ; i < A.length ; i++){           if(A[i] == pivot){               index++;           }else{                   A[i - index] = A[i];           }        }                return A.length - count;    }}

0 0
原创粉丝点击