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.
Ideas:
Additional space is not allowed, but it can be used regardless of the return length of the array, so the returned part of the array can be re-assigned to an array with no duplicates, returning its length.
Solution:
1 Public classSolution2 {3 Public intRemoveDuplicates (int[] nums)4 {5 if(Nums.length <= 1)6 returnnums.length;7 8 intLen = 1;9 for(inti = 1; i < nums.length; i++)Ten { One if(Nums[i]! = nums[i-1]) A { -Nums[len] =Nums[i]; -len++; the } - } - - returnLen; + } -}
Leetcode Remove duplicates from Sorted Array