Remove Duplicates from Sorted Array

##题目

####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].

##解题思路
该题是去除数组中的多余的重复元素。与Remove Element类似。做法是维护两个指针,一个保留当前有效元素的长度,一个从前往后扫,然后跳过那些重复的元素。因为数组是有序的,所以重复元素一定相邻,不需要额外记录。时间复杂度是O(n),空间复杂度O(1)。

##算法代码
代码采用JAVA实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
public int removeDuplicates(int[] A) {
int len=A.length;
if(len==0) return 0;
if(len==1) return 1;
int i=0,j=1;
while(j<len){
if(A[i]!=A[j])
{
A[i+1]=A[j];
i++;
j++;
}else{
j++;
}
}
return i+1;
}
}

Comments