(26)Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

 1     public static int removeDuplicates(int[] A) {
 2         if (A == null || A.length == 0)
 3             return 0;
 4         int size = 0;
 5         for (int i = 0; i < A.length; i++) {
 6             if (A[i] != A[size])
 7                 A[++size] = A[i];
 8         }9        return size + 1;
10     }

 https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/

posted on 2015-02-11 15:38  lh_chuang  阅读(99)  评论(0)    收藏  举报

导航