LeetCode 题解 | 80. 删除排序数组中的重复项 II
off999 2024-11-06 11:22 25 浏览 0 评论
力扣 80. 删除排序数组中的重复项 II (点击查看题目)
题目描述
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
示例 1:
示例 2:
说明:
为什么返回数值是整数,但输出的答案是数组呢?
请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
你可以想象内部操作如下:
解决方案
方法一:删除多余的重复项
由于输入数组已经排序,所以重复项都显示在旁边。题目要求我们不使用额外的空间,在原地修改数组,而最简单的方法就是删除多余的重复项。对于数组中的每个数字,若出现 2 个以上的重复项,就将多余的重复项从数组列表中删除。
算法:
- 我们需要在遍历数组元素的同时删除多余的重复项,那么我们需要在删除多余重复项的同时更新数组的索引,否则将访问到无效的元素或跳过需要访问的元素。
- 我们使用两个变量,i 是遍历数组的指针,count 是记录当前数字出现的次数。count 的最小计数始终为 1。
- 我们从索引 1 开始一次处理一个数组元素。
- 若当前元素与前一个元素相同,即 nums[i]==nums[i-1],则增加计数 count++。若 count > 2,则说明遇到了多余的重复元素 ,要从数组中删除它。由于我们知道这个元素的索引,可以使用 del,pop 或 remove 操作(或你选择语言支持的任何相应的操作)从数组中删除它。由于删除了一个元素,所以我们的索引应该要减一。
- 若当前元素与前一个元素不相同,即 nums[i] != nums[i - 1],说明我们遇到了一个新元素,则更新 count = 1。
- 由于我们从原始数组中删除了所有多余的重复项,所以最终在原数组只保留了有效元素,返回数组长度。
Python 实现(可在电脑端查看代码)
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# Initialize the counter and the array index.
i, count = 1, 1
# Start from the second element of the array and process
# elements one by one.
while i < len(nums):
# If the current element is a duplicate,
# increment the count.
if nums[i] == nums[i - 1]:
count += 1
# If the count is more than 2, this is an
# unwanted duplicate element and hence we
# remove it from the array.
if count > 2:
nums.pop(i)
# Note that we have to decrement the
# array index value to keep it consistent
# with the size of the array.
i-= 1
else:
# Reset the count since we encountered a different element
# than the previous one
count = 1
# Move on to the next element in the array
i += 1
return len(nums)Java 实现(可在电脑端查看代码)
class Solution {
public int[] remElement(int[] arr, int index) {
//
// Overwrite the element at the given index by
// moving all the elements to the right of the
// index, one position to the left.
//
for (int i = index + 1; i < arr.length; i++) {
arr[i - 1] = arr[i];
}
return arr;
}
public int removeDuplicates(int[] nums) {
// Initialize the counter and the array index.
int i = 1, count = 1, length = nums.length;
//
// Start from the second element of the array and process
// elements one by one.
//
while (i < length) {
//
// If the current element is a duplicate,
// increment the count.
//
if (nums[i] == nums[i - 1]) {
count++;
//
// If the count is more than 2, this is an unwanted duplicate element
// and hence we remove it from the array.
//
if (count > 2) {
this.remElement(nums, i);
//
// Note that we have to decrement the array index value to
// keep it consistent with the size of the array.
//
i--;
//
// Since we have a fixed size array and we can't actually
// remove an element, we reduce the length of the array as
// well.
//
length--;
}
} else {
//
// Reset the count since we encountered a different element
// than the previous one.
//
count = 1;
}
// Move on to the next element in the array
i++;
}
return length;
}
}复杂度分析
- 时间复杂度:让我们看看最耗时的操作是什么:
- 我们必须遍历数组中的所有元素,若数组中有 N 个元素,则花费的时间为 O(N)。
- 删除多余的重复项,del 操作也是 O(N)。
- 最糟糕的情况是数组中的元素都相同,则我们需要执行 N - 1 次的删除操作,则需要花费 O(N^2)。
- 总的复杂度:
- 空间复杂度:O(1),我们在原地修改数组。
方法二:覆盖多余的重复项
算法:
- 我们使用了两个指针,i 是遍历指针,指向当前遍历的元素;j 指向下一个要覆盖元素的位置。
- 同样,我们用 count 记录当前数字出现的次数。count 的最小计数始终为 1。
- 我们从索引 1 开始一次处理一个数组元素。
- 若当前元素与前一个元素相同,即 nums[i]==nums[i-1],则 count++。若 count > 2,则说明遇到了多余的重复项。在这种情况下,我们只向前移动 i,而 j 不动。
- 若 count <=2,则我们将 i 所指向的元素移动到 j 位置,并同时增加 i 和 j。
- 若当前元素与前一个元素不相同,即 nums[i] != nums[i - 1],说明遇到了新元素,则我们更新 count = 1,并且将该元素移动到 j 位置,并同时增加 i 和 j。
- 当数组遍历完成,则返回 j。
Python 实现
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# Initialize the counter and the second pointer.
j, count = 1, 1
# Start from the second element of the array and process
# elements one by one.
for i in range(1, len(nums)):
# If the current element is a duplicate,
# increment the count.
if nums[i] == nums[i - 1]:
count += 1
else:
# Reset the count since we encountered a different element
# than the previous one
count = 1
# For a count <= 2, we copy the element over thus
# overwriting the element at index "j" in the array
if count <= 2:
nums[j] = nums[i]
j += 1
return j
Java 实现
class Solution {
public int removeDuplicates(int[] nums) {
//
// Initialize the counter and the second pointer.
//
int j = 1, count = 1;
//
// Start from the second element of the array and process
// elements one by one.
//
for (int i = 1; i < nums.length; i++) {
//
// If the current element is a duplicate, increment the count.
//
if (nums[i] == nums[i - 1]) {
count++;
} else {
//
// Reset the count since we encountered a different element
// than the previous one.
//
count = 1;
}
//
// For a count <= 2, we copy the element over thus
// overwriting the element at index "j" in the array
//
if (count <= 2) {
nums[j++] = nums[i];
}
}
return j;
}
}复杂度分析
- 时间复杂度:O(N),我们遍历每个数组元素一次。
- 空间复杂度:O(1)。
本文作者:力扣
声明:本文归“力扣”版权所有,如需转载请联系。
相关推荐
- 戴尔官网保修查询入口(戴尔售后保质期查询)
-
可以按照以下步骤查询戴尔笔记本电脑的保修期:1.打开戴尔官网:https://www.戴尔.com/zh-cn/售后服务/保修政策.html2.点击页面上方的“服务与支持”按钮,进入戴尔的服务支持...
- 手机号邮箱登录入口(手机号邮箱官网)
-
手机163邮箱登录入口如下:163邮箱官网入口:https://smart.mail.163.com/login.htm点击进入登录或者注册邮箱即可。手机浏览器访问进入官网http://www.123...
- sd卡(sd卡无法读取怎么修复)
-
SD卡是大卡,相机用的;普通的手机内存卡,是小卡,正规的名称是macrosd卡,也就是微型SD卡。可以通过卡套转为普通的SD卡的大小。 其实就是大小不同。但手机上的内存卡,人们经常也俗称为SD...
- windows7蓝牙功能在哪里打开
-
点击搜索框在windows7系统主界面点击开始菜单,点击打开搜索框。输入命令输入services.msc后回车,在列表中找到并右击BluetoothSupportS...点击属性选择进入属性菜单,...
-
- 2010激活密钥(microsoft2010激活密钥)
-
步骤/方式1officeprofessionalplus2010:(office专业版)6QFdx-pYH2G-ppYFd-C7RJM-BBKQ8Bdd3G-xM7FB-Bd2HM-YK63V-VQFdKVYBBJ-TRJpB-QFQ...
-
2025-11-19 04:03 off999
- 联想官方刷新bios工具(联想电脑刷新bios)
-
刷新BIOS需要使用联想的官方网站或授权维修中心来进行操作。以下是一些基本步骤:1.访问联想的官方网站,找到BIOS更新程序并下载。在下载过程中,请确保选择与您计算机型号匹配的版本。2.将下载的B...
-
- 苹果ios14系统下载(苹果ios14.1下载)
-
1方法一步骤/方式一打开Appstore。步骤/方式二在搜索栏点击搜索框。步骤/方式三搜索并点击需要下载的软件。步骤/方式四点击获取。步骤/方式五最后验证ID密码即可。1.在应用商店搜索你要下载的应用名称。2.点击下载按钮,如果要求登...
-
2025-11-19 03:03 off999
- office2010怎么免费永久激活密钥
-
用这个试试,一个KMS激活工具可以激活2010到2019的Office自家的目前用的就是这个microsoft6477.moe/1716.html直接使用这个Microsoftoffice2010...
-
- 类似爱加速的国内ip(类似爱加速的app)
-
推荐“V8盒子”。这一款免费无广告的模拟器,不同于其它软件盒子,而是类似于X8沙箱,满足游戏多开,画中画,悬浮球操作,熄屏后台运行等多功能的沙箱盒子.支持一键root,一键安装xposed框架,能在安卓/苹果手机上运行多个安卓/ios虚拟系...
-
2025-11-19 02:03 off999
- 阿里旺旺手机客户端(阿里旺旺手机app)
-
手机淘宝的旺旺在打开商品后,会看到左下角有个旺旺的图标,点击就可以联系了。 阿里旺旺是将原先的淘宝旺旺与阿里巴巴贸易通整合在一起的一个新品牌。它是淘宝和阿里巴巴为商人量身定做的免费网上商务沟通软件,...
- 最纯净的pe装机工具(pe工具哪个纯净)
-
U盘装系统步骤:1.制作U盘启动盘。这里推荐大白菜U盘启动盘制作工具,在网上一搜便是。2.U盘启动盘做好了,我们还需要一个GHOST文件,可以从网上下载一个ghost版的XP/WIN7/WIN8系统,...
- 装一个erp系统多少钱(wms仓库管理软件)
-
现在主流有客户端ERP和云端ERP两种客户端通常一次买断,价格在万元左右,但是还有隐性费用,你需要支付服务器、数据管理员,此外如果系统需要更新维护,你还需要支付另外一笔不菲的费用。云端ERP:优势...
- cad2014序列号和密钥永久(autocad2014序列号和密钥)
-
1在cad2014中修改标注样式后,需要将其保存2单击“样式管理器”按钮,在弹出的窗口中选择修改后的标注样式,然后单击“设置为当前”按钮,再单击“保存当前样式”按钮,将其保存为新的样式名称3为了...
- qq修改密保手机号(qq修改密保手机号是什么意思)
-
QQ更改绑定的手机号码操作步骤如下:1、打开手机主界面,找到“QQ”软件点击打开。2、输入正确的QQ账户和密码登录到qq主界面。3、点击左上角的头像“图片”,进入到个人中心界面。4、进入到个人中心界面...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
python入门到脱坑 输入与输出—str()函数
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
宝塔面板如何添加免费waf防火墙?(宝塔面板开启https)
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
慕ke 前端工程师2024「完整」
-
失业程序员复习python笔记——条件与循环
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
