当前位置:网站首页>[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)
边栏推荐
- 你真的了解Redis的持久化机制吗?
- 【2023校招刷题】常见面试问题总结(七、常见总线协议篇)(随后续面试不断更新....)
- @Autowired与@Resource区别
- 文档贡献与写作必读-OpenHarmony开发者文档风格指南
- Analysis of miscellaneous diseases such as DNS domain name hijacking in instant messaging mobile terminal development
- Embedded system driver primary [1] - kernel module _ compilation method
- VsCode更新后,怎么使用使用快捷键同时生成多个元素
- 《MySQL DBA封神打怪之路》专栏学习大纲
- kaniko --customPlatform参数:支持不同平台的镜像构建(如:arm等)
- high-level-rest-client 判断索引是否存在
猜你喜欢
[leetcode] 82. Delete duplicate elements in sorted linked list II (medium)
Baidu Intelligent Cloud Zhangmiao: Detailed explanation of enterprise-level seven-layer load balancing open source software BFE
Analysis of miscellaneous diseases such as DNS domain name hijacking in instant messaging mobile terminal development
【leetcode】82. 删除排序链表中的重复元素 II(中等)
@Accessors 注解详解
Qt uses QSortFilterProxyModel for sorting and filtering in QML
PyCharm使用教程(详细版 - 图文结合)
DNA偶联二维过渡金属硫化物|DNA修饰贵金属纳米颗粒|使用方法
MySQL面试题:用户金额充值面试题详解
【微信小程序】一文解忧,事件绑定
随机推荐
使用 Neuron 接入 Modbus TCP 及 Modbus RTU 协议设备
JZ22 链表中倒数最后k个结点
MySQL面试题:用户金额充值面试题详解
互联网基石:TCP/IP四层模型,由浅入深直击原理!
Spark读取多目录
你真的了解Redis的持久化机制吗?
【leetcode】50. Pow(x, n)(中等)(快速幂)
50. Leetcode 】 【 Pow (x, n) (medium) (power) quickly
Single chip ds1302 clock program (51 single chip liquid crystal display program)
WeChat applet sliding navigation bar (how to set the floating window of the webpage)
kaniko --customPlatform参数:支持不同平台的镜像构建(如:arm等)
Jsp使用&lt;c:forEach&gt;遍历List集合「建议收藏」
Professor Lu Shouqun from COPU was invited to give a speech at ApacheCon Asia
C语言快速入门(为了看源码)
汉字的URL转义字符函数
将文件流转成file文件后使用luckysheet回显数据
Qt uses QSortFilterProxyModel for sorting and filtering in QML
Analysis of miscellaneous diseases such as DNS domain name hijacking in instant messaging mobile terminal development
Redis和MySQL如何保持数据一致性
JZ24 反转链表