Leetcode 80. Remove Duplicates from Sorted Array II
Leetcode 26的加强版,只需要多加一个变量记录重复次数.
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ size=len(nums) if size in [0,1,2]: return size i=0 n=1 for j in range(1,size): if nums[j]==nums[i]: if n==1: i+=1 nums[i]=nums[j] n+=1 elif nums[j]!=nums[i]: i+=1 nums[i]=nums[j] n=1 return i+1