leetcode.array--80. Remove Duplicates from Sorted Array II

来源:互联网 发布:ubuntu 启动脚本 编辑:程序博客网 时间:2024/05/22 20:10

题目:80. Remove Duplicates from Sorted Array II

题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/

从给定的有序数组中移除重复元素,每个元素最多可以出现两次。

Python:

class Solution(object):    def removeDuplicates(self, nums):        """        :type nums: List[int]        :rtype: int        """        length=len(nums)        if length<3:            return length        cur,cnt = 0,1        for i in range(1,length):            if nums[i]==nums[cur]:                if cnt<2:                    cur+=1                    cnt+=1                    nums[cur]=nums[i]                    continue                else:                    continue            else:                cur+=1                nums[cur]=nums[i]                cnt=1        return cur+1


阅读全文
0 0