Python每日一练---第三天:删除有序数组中的重复项

前言📢📢

Python每日一练来啦,本文已收录于:《Python每日一练》专栏

此专栏目的在于,帮忙学习Python的小白提高编程能力,训练逻辑思维,持续更新中,欢迎免费订阅!!!
*

1. 问题描述

给你一个有序数组 nums ,请你原地 删除重复出现的元素,使得出现次数超过两次的元素只出现两次 ,返回删除后数组的新长度。

不要使用额外的数组空间,你必须在 原地 修改输入数组并在使用 O(1) 额外空间的条件下完成。

示例 1:

输入:nums = 1,1,1,2,2,3 输出:5, nums = 1,1,2,2,3 解释:函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3。 不需要考虑数组中超出新长度后面的元素。

示例 2:

输入:nums = 0,0,1,1,1,1,2,3,3 输出:7, nums = 0,0,1,1,2,3,3 解释:函数应返回新长度 length = 7, 并且原数组的前七个元素被修改为 0, 0, 1, 1, 2, 3, 3。不需要考虑数组中超出新长度后面的元素。
*

2. 问题分析

使用滑动窗口+删除元素的方法。

对于nums = 1,1,1,2,2,3

(1)窗口1,1,1, 长度=3>2,删除第3个1---->1,1,2,2,3

(2)窗口2,2,长度=2<=2,保留

(3)窗口3,长度=1<=2,保留

结果:1,1,2,2,3, 长度=5
*

3. 算法思路

思路:

(1)定义窗口: beginIndex和endIndex标记相同元素的起始和结束位置

(2)遍历数组:用endIndex向右扩展,找到相同元素的连续区间

(3)处理重复:当遇到不同元素时,检查当前连续区间的长度

如果长度>2,删除多余的元素

如果长度<=2,直接移动指针
*

4. 代码实现

python 复制代码
from typing import List

class Solution:
    def remove(self, nums: List[int]) -> int:
        if len(nums) <= 2:
            return len(nums)

        slow = 2  # 从第三个位置开始检查

        for fast in range(2, len(nums)):
            # 如果当前元素不等于slow指针前两个位置的元素
            # 说明可以保留当前元素
            if nums[fast] != nums[slow - 2]:
                nums[slow] = nums[fast]
                slow += 1

        return slow

    def removeDuplicates(self, nums: List[int]) -> int:
        if not nums:
            return 0
        beginIndex = 0
        endIndex = 0
        value = nums[beginIndex]
        while endIndex < len(nums):
            if nums[endIndex] == value:
                endIndex += 1
            else:
                if endIndex - beginIndex > 2:
                    for i in range(endIndex-1, beginIndex+1, -1):
                        nums.pop(i)
                    beginIndex = beginIndex + 2
                    endIndex = beginIndex
                    value = nums[beginIndex]
                else:
                    beginIndex = endIndex
                    value = nums[beginIndex]

        if endIndex - beginIndex > 2:
            for i in range(endIndex-1, beginIndex+1, -1):
                nums.pop(i)
        return len(nums)

if __name__ == '__main__':
    #print(Solution().removeDuplicates([1,1,1]))
    #print(Solution().removeDuplicates([1,1,1,2,2,3]))
    print(Solution().removeDuplicates([0,0,1,1,1,1,2,3,3]))

这个算法的代码思路直观,容易理解,使用滑动窗口的概念,但是时间复杂度高,pop(i)操作是O(n),最坏情况时间复杂度是O(n^2),并且需要处理多个边界情况,频繁删除操作导致数组元素频繁移动。可以尝试使用双指针法,时间复杂度降为O(n).

相关推荐
花酒锄作田16 小时前
Pydantic校验配置文件
python
hboot16 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉
韩师傅2 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L2 天前
LangGraph的MessageState and HumanMessage
python