当前位置:网站首页>[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)
边栏推荐
- The Sandbox 与 Gravity 达成合作,将《RO仙境传说》带入元宇宙
- JZ39 数组中出现次数超过一半的数字
- pnpm + workspace + changesets 构建你的 monorepo 工程
- Codeforces Round #245 (Div. 1) A (dfs)
- LabVIEW为什么在存储VI时死机
- Single chip ds1302 clock program (51 single chip liquid crystal display program)
- i2c时序图的详细讲解[通俗易懂]
- J9 Number Theory: Why do we need Web3?
- 2022年最新甘肃建筑施工焊工(建筑特种作业)模拟题库及答案解析
- ah?Now the primary test recruitment requirements will be automated?
猜你喜欢

BGP Federal Comprehensive Experiment

【MySQL系列】 MySQL表的增删改查(进阶)

Another new rule for credit cards is coming!Juphoon uses technology to boost the financial industry to improve service quality and efficiency

【openlayers】地图【二】

Qt uses QSortFilterProxyModel for sorting and filtering in QML

DNA修饰纳米金颗粒|DNA脱氧核糖核酸偶联修饰碳纳米材料|实验原理

DNA修饰碱基5-甲基胞嘧啶和8-羟基鸟嘌呤|DNA修饰量子点|规格信息

Design for failure常见的12种设计思想

Implementation and implementation of Any to Any real-time voice change丨RTC Dev Meetup

BGP联邦综合实验
随机推荐
把字符串转换成整数
canvas 中如何实现物体的点选(五)
【企业架构框架】谁推动了现代 EA 最佳实践和内容?
cached_network_image crashes with multiple images
Analysis of miscellaneous diseases such as DNS domain name hijacking in instant messaging mobile terminal development
一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
JZ39 数组中出现次数超过一半的数字
基于zigbee的智能管理系统[通俗易懂]
Topics in Dynamic Programming
【leetcode】50. Pow(x, n)(中等)(快速幂)
MQTT over QUIC:下一代物联网标准协议为消息传输场景注入新动力
This article penetrates the architecture design and cluster construction of the distributed storage system Ceph (hands-on)
Access the company intranet
Redis和MySQL如何保持数据一致性
cmd md命令
Codeforces Round #245 (Div. 1) A (dfs)
esp12f + tft 显示图片问题
2022年最新甘肃建筑八大员(材料员)模拟考试试题及答案
云计算1+X之openstack篇
Professor Lu Shouqun from COPU was invited to give a speech at ApacheCon Asia