当前位置:网站首页>[leetcode] 80. Delete duplicates in sorted array II (medium) (double pointer, in-place modification)
[leetcode] 80. Delete duplicates in sorted array II (medium) (double pointer, in-place modification)
2022-07-29 23:16:00 【friedrichor】
80. 删除有序数组中的重复项 II
问题描述
给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使得出现次数超过两次的元素只出现两次 ,返回删除后数组的新长度.
不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成.
说明:
为什么返回数值是整数,但输出的答案是数组呢?
请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的.
你可以想象内部操作如下:
// nums 是以“引用”方式传递的.也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);
// 在函数里修改输入数组对于调用者是可见的.
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
示例 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 . 不需要考虑数组中超出新长度后面的元素.
提示:
- 1 <= nums.length <= 3 * 104
- -104 <= nums[i] <= 104
- nums 已按升序排列
思路&代码
The point of this question is still原地修改
方法一:
A dictionary can be used to keep track of the number of occurrences of each number,用一个指针pto record the current position that needs to be changed,指针i用来遍历.However, this method uses a dictionary,Therefore, the additional space complexity is higher.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
p = 0
dic = dict()
for i in range(len(nums)):
n_now = nums[i]
if n_now not in dic.keys():
dic[n_now] = 1
else:
dic[n_now] += 1
if dic[n_now] <= 2:
nums[p] = n_now
p += 1
nums = nums[:p]
return len(nums)
方法二:
仍然是 p 和 i 两个指针,However, the condition of judgment here is changed to comparison n u m s [ i ] nums[i] nums[i] 和 n u m s [ p − 2 ] nums[p-2] nums[p−2] 是否相同,The same means that the number is greater than3个了,应该从 nums This redundant part has been removed.In this case the extra space is constant,即 O ( 1 ) O(1) O(1),满足条件.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
p = 2
for i in range(2, len(nums)):
if nums[i] != nums[p-2]:
nums[p] = nums[i]
p += 1
nums = nums[:p]
return len(nums)
边栏推荐
猜你喜欢

We launched a "developer lab"
![Win7x64中使用PowerDesigner连接Oralce数据库报“[Oracle][ODBC][Ora]ORA-12154:TNS:无法解析指定的连接标识符”错误解决方法](/img/47/9d17f3026b862927af8917f3a8fcad.png)
Win7x64中使用PowerDesigner连接Oralce数据库报“[Oracle][ODBC][Ora]ORA-12154:TNS:无法解析指定的连接标识符”错误解决方法

地狱挖掘者系列#1

新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间

MySQL active/standby switch

浅析即时通讯移动端开发DNS域名劫持等杂症

【openlayers】地图【二】

MySQL面试题:用户金额充值面试题详解
![[leetcode] 82. Delete duplicate elements in sorted linked list II (medium)](/img/93/a744cfc059245de2fc07894167f3c5.png)
[leetcode] 82. Delete duplicate elements in sorted linked list II (medium)

BGP联邦综合实验
随机推荐
jenkins使用维护
MySQL active/standby switch
【2023校招刷题】常见面试问题总结(七、常见总线协议篇)(随后续面试不断更新....)
真offer收割机 第一弹~大厂如何考察候选人?(附答案详解)
MySQL面试题:用户金额充值面试题详解
Implementation and implementation of Any to Any real-time voice change丨RTC Dev Meetup
你真的了解Redis的持久化机制吗?
DNA脱氧核糖核酸修饰石墨粉末|DNA修饰还原石墨烯功能材料|保存温度
汉字的URL转义字符函数
JZ22 链表中倒数最后k个结点
基于zigbee的智能管理系统[通俗易懂]
canvas 中如何实现物体的点选(五)
High - level - the rest - the client determine whether indexes exist
一个print函数,挺会玩啊?
MySQL数据库进阶篇
@Autowired与@Resource区别
DNA脱氧核糖核酸修饰四氧化三铁|DNA修饰氧化锌|使用方法
纳米金颗粒修饰核酸产品|碳纳米管载核酸-DNA/RNA材料|解析说明
JZ39 数组中出现次数超过一半的数字
JetsonNano学习(五)JetsonNano 安装 PyTorch 及 Torchvision