[Problem]
[Solution]
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]
.
[Solution]
class Solution {说明:版权所有,转载请注明出处。 Coder007的博客
public:
int removeDuplicates(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(n <= 1)return n;
int i = 0, j = 0;
while(j < n){
if(j > i && A[i] != A[j]){
A[++i] = A[j];
}
j++;
}
return i+1;
}
};