【LeetCode OJ】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 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 if(!n) 5 return NULL; 6 int i,num=1; 7 for(i=1;i<n;i++) 8 { 9 if(A[i]!=A[i-1]) 10 { 11 A[num++]=A[i]; 12 } 13 } 14 return num; 15 } 16 };