Follow up for "Remove duplicates":
What if duplicates is allowed at the most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3]
,
Your function should return length =, with the first 5
five elements of nums being 1
, 1
2
2
,, and 3
. It doesn ' t matter what are you leave beyond the new length.
is the 26th version of the extension, 26 is to give a sorted array, and then delete all the duplicate numbers, so that all the numbers appear only 1 times, and then return the length.
This problem refers to allowing a number to appear up to 2 times. Then seek the length len2. And the number of LEN2 before the returned array should be the array after the collation.
Public classSolution { Public intRemoveDuplicates (int[] nums) { intLen =nums.length; if(Len < 2 ) returnLen; intFlag = Nums[0]; intTimes = 1; intres =Len; for(inti = 1, j = 1;i<len;i++){ if(Flag = =Nums[i]) { if(Times = = 1) Times++; Else{res--; Continue; } }Else{flag=Nums[i]; Times= 1; } Nums[j]=Nums[i]; J++; } returnRes; }}
Leetcode Remove duplicates from Sorted Array II-----java