Problem:
Given a sorted array, remove the duplicates in place such, all element appear only once and return the new L Ength.
Do the allocate extra space for another array, and you must does this on place with constant memory.
For example,
Given input array nums = [1,1,2]
,
Your function should return length = 2
, with the first of the elements of nums being and 1
2
Respectivel Y. It doesn ' t matter what are you leave beyond the new length.
Analysis:
Removes the repeating number in the sorted array and returns the number of digits contained in the final array.
Summary:
With two pointers, the pointer I iterates through the array, and the pointer J marks the end of the currently removed-weight array, each traversing to a new number and placing it in J.
1 classSolution {2 Public:3 intRemoveDuplicates (vector<int>&nums) {4 intLen =nums.size ();5 if(!Len) {6 return 0;7 }8 9 intj =1;Ten for(inti =1; i < Len; i++) { One if(Nums[i-1] !=Nums[i]) { ANums[j++] =Nums[i]; - } - } the - returnJ; - } -};
Leetcode Remove duplicates from Sorted Array