当前位置:网站首页>LeetCode26:删除有序数组中的重复项
LeetCode26:删除有序数组中的重复项
2022-08-09 21:35:00 【哆啦k梦0219】
给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。
由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应该保存最终结果。
将最终结果插入 nums 的前 k 个位置后返回 k 。
不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-array
本题目采用双指针法,在力扣评论区看到这样一段话非常有意思,可以帮助我们理解这个双指针法:

双指针法 (C++)
双指针法在数组、链表题目中都有很大的用处。
class Solution {
public:
int removeDuplicates(vector<int>& nums)
{
int fast = 1;//快指针
int slow = 0;//慢指针
if(nums.size()==0)
return 0;
for(fast = 1;fast<nums.size();fast++)//注意C++容器中是.size()获得容量,没有.length()
{
if(nums[fast]!=nums[slow])
{
slow++;
nums[slow]=nums[fast];
}
}
return slow+1;//长度 = 下标+1
}
};边栏推荐
- Next second data: the transformation of the modern data stack brought about by the integration of lake and warehouse has begun
- windos安装Mysql8.0,及解决重新登录异常问题 ERROR 1045 (28000)
- Lyapp exponents and bifurcation diagrams for fractional chaotic systems
- 字节一面:TCP 和 UDP 可以使用同一个端口吗?
- Acrel5000web能耗系统在某学院的应用-Susie 周
- Beat the interviewer, the CURD system can also make technical content
- cadence中复制一部分PCB到另一个PCB中去
- MySQL跨表、多表更新SQL语句总结
- PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
- DSPE-PEG-PDP,DSPE-PEG-OPSS,磷脂-聚乙二醇-巯基吡啶可减少肽的免疫原性
猜你喜欢

URL Protocol web page to open the application

小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和

Ali Ermi: Without accept, can a TCP connection be established?

小黑leetcode之旅:94. 二叉树的中序遍历(补充Morris 中序遍历)

How to fix Windows 11 not finding files

Word怎么制作一张标准的答题卡?

knn到底咋回事?

MySQL慢查询的多个原因

How are data integration APIs key to enterprise digital transformation?

Error when source install/setup.bash
随机推荐
【stack】【queue】【priority_queue】【deque】详解
matlab neural network ANN classification
蓝牙模块有哪些种类?BLE低功耗蓝牙模块有什么特点?
Word第一页不要页眉怎么设置?设置Word首页不要页眉方法教程
字节一面:TCP 和 UDP 可以使用同一个端口吗?
访问控制知识
PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
单元测试
Deceptive Dice(期望计算)
微软Excel表格点击单元格行和列都显示颜色怎么弄?聚光灯效果设置
数据集成API如何成为企业数字化转型的关键?
企业数据打通有什么好处?不同行业怎么解决数据打通难题?
筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
How to fix Windows 11 not finding files
PHP 二维数组根据某个字段排序
LED闪烁 闪灯芯片IC 手电筒IC 闪灯控制IC 闪烁IC流水灯
XXE-XML外部实体注入-知识点
【双链表增删查改接口的实现】
Problems with compiling SIP with QGIS
【高效工具】远程控制软件 ToDesk(收藏夹)