【LeetCode】删除排序数组中的重复项 II

题目

链接

思路

双指针

我好聪明啊,自己想出了这个双指针的办法,哈哈哈哈哈哈哈,太高兴了

代码

python 复制代码
class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n=len(nums)
        if n<=1:
            return n

        left,right=0,0
        repeat={}
        while right<n:
            x=nums[right]
            if x not in repeat:
                repeat[x]=1
            else:
                repeat[x]+=1

            if repeat[x]<=2:
                nums[left]=x
                left+=1

            right+=1

        return left


solution=Solution()
input_content=[0,0,1,1,1,1,2,3,3]
ans=solution.removeDuplicates(input_content)
print(ans)
相关推荐
2401_8384725112 小时前
C++模拟器开发实践
开发语言·c++·算法
s1hiyu12 小时前
实时控制系统验证
开发语言·c++·算法
daad77712 小时前
V4L2_mipi-csi
算法
2301_7657031412 小时前
C++代码复杂度控制
开发语言·c++·算法
m0_7088309612 小时前
C++中的享元模式实战
开发语言·c++·算法
naruto_lnq13 小时前
分布式计算C++库
开发语言·c++·算法
m0_7066532313 小时前
模板编译期排序算法
开发语言·c++·算法
历程里程碑13 小时前
Linxu14 进程一
linux·c语言·开发语言·数据结构·c++·笔记·算法
木井巳13 小时前
【递归算法】验证二叉搜索树
java·算法·leetcode·深度优先·剪枝
m0_5613596713 小时前
嵌入式C++加密库
开发语言·c++·算法