当前位置:网站首页>One question of the day - delete duplicates of the ordered array
One question of the day - delete duplicates of the ordered array
2022-06-11 21:57:00 【Code loving students】
Title Description :
To give you one An array arranged in ascending order nums , Please delete the repeated elements in place , Make each element appear only once , Returns the new length of the deleted array . The relative order of elements should be maintained Agreement .
Because the length of an array cannot be changed in some languages , So you have to put the result in an array nums The first part of . More formally , If there is... After deleting duplicates k Elements , that nums Before k An element should hold the final result .
Insert the final result into nums Before k Return to... After a position k .
Don't use extra space , You must be there. In situ Modify input array And using O(1) Complete with extra space .
Topic analysis :
For this problem, we can use the double pointer method to , Definition fast and slow Two pointers , If the array arr[fast]!=arr[fast-1] shows fast Is the boundary of non duplicates , Then its value can be assigned .
The code is as follows :
int removeDuplicates(int* nums, int numsSize){
// from 1 It starts with the subscript 0 There is no need to delete the element of
int fast=1,slow=1;
while(fast<numsSize){
if(nums[fast]!=nums[fast-1]){
nums[slow]=nums[fast];
slow++;
}
fast++;
}
return size;
}边栏推荐
- go io模块
- Redis basic data type (hash)
- All features of polymorphism
- Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?
- The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace
- Leetcode-98- validate binary search tree
- 快速排序的三种方法
- go os模块
- LaTex实战笔记 3-宏包与控制命令
- R language book learning 03 "in simple terms R language data analysis" - Chapter 12 support vector machine Chapter 13 neural network
猜你喜欢

类和对象(4)

打印机无法打印测试页是什么原因

高考结束,人生才刚刚开始,10年职场老鸟给的建议

Leetcode-98- validate binary search tree

Endnotex9 introduction and basic tutorial instructions

On the night of the joint commissioning, I beat up my colleagues

如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称

Take off efficiently! Can it be developed like this?

189. rotation array

Matlab: 文件夹锁定问题的解决
随机推荐
Look for leap years and see how many leap years I have had since I was born (I have had five)
Relatively perfect singleton mode
R language book learning 03 "in simple terms R language data analysis" - Chapter 8 logistic regression model Chapter 9 clustering model
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
华为设备配置HoVPN
Sword finger offer 29 Print matrix clockwise
189. rotation array
Nmap进行主机探测出现网段IP全部存活情况分析
Game client performance (memory) [previous]
LabVIEW Arduino electronic weighing system (project Part-1)
Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?
Experiment 10 Bezier curve generation - experiment improvement - interactive generation of B-spline curve
Flink error: multiple tasks are started, and only one task is executed
Take off efficiently! Can it be developed like this?
Go IO module
效率起飞啊!还能这样开发的?
如何查看win系统的安装日期
相对完善的单例模式
Latex combat notes 3- macro package and control commands
[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?