(26)Remove Duplicates from Sorted Array

时间:2015-02-11 16:18:27   收藏:0   阅读:255

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/

原文:http://www.cnblogs.com/luochuanghero/p/4286371.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!