Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?
Example
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.
classSolution(object):defremoveDuplicates(self,nums):""" :type nums: List[int] :rtype: int """ifnums==Noneorlen(nums)==0:return0start=0# first element nums[end] != nums[start]end=0index=0whilestart<len(nums):end=self.findRec(nums,start)nums[index]=nums[start]index+=1ifend-start>=2:nums[index]=nums[start]index+=1start=endreturnindexdeffindRec(self,nums,start):whilestart+1<len(nums):ifnums[start]!=nums[start+1]:returnstart+1start+=1returnstart+1