Remove Duplicates from Sorted Array
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.
Test instructions
Given an array of nums[], calculate the length of the data after it is removed (no new memory space is opened)
Problem Solving Ideas:
Set two flag bits left=0;right=1
Use left and right to compare, if the same skip right to continue, if not the same then put right in the back position
1 Public classSolution {2 Public intRemoveDuplicates (int[] nums) {3 intlen=nums.length;4 if(len==0)5 return0;6 intI=0; 7 for(intj=1;j<len;j++){8 if(nums[i]!=Nums[j]) {9i++; Tennums[i]=Nums[j]; One A } - - } the returnI+1; - } -}
Remove duplicates from Sorted array "Leetcode", array, Array,java, algorithm