【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)
相关推荐
liliangcsdn7 分钟前
IVOL与偏度因子的对比测量分析
算法
threerocks1 小时前
Jev 入门第一课
算法
西柚研究生1234562 小时前
论文分析17:YOLOv11_UAVNet:无人机航拍图像专用目标检测算法
人工智能·python·深度学习·算法·目标检测
hetao17338373 小时前
2026-09-17 hetao1733837 的刷题记录
c++·算法
午彦琳4 小时前
2026.9.17
数据结构·算法·leetcode
木井巳4 小时前
【记忆化搜索】不同路径
java·算法·leetcode·深度优先·剪枝·推荐算法
怕浪猫5 小时前
从 Windows 换到 Mac 三个月,我真香了
算法·面试·架构
aichitang20246 小时前
前端小skill
前端·人工智能·算法·ai·前端框架
All for pursuit.7 小时前
【链表-9】146.LRU缓存
数据结构·c++·算法·leetcode
木子算法7 小时前
测出来的值会抖:约束和目标带噪声时,「可行」和「更好」该怎么判
人工智能·算法·目标跟踪