当前位置:网站首页>Leetcode daily practice - 26. Delete duplicates in an ordered array
Leetcode daily practice - 26. Delete duplicates in an ordered array
2022-07-26 19:49:00 【An airliner flying to the stars】
Preface
Wassup guys! I am a Edison
It's today LeetCode Upper leetcode 26. Remove duplicate items from an ordered array
Let’s get it!

List of articles
1. Topic analysis
To give you one Ascending order Array of nums , Would you please In situ Delete duplicate elements , Make each element Only once , Returns the new length of the deleted array . Elemental Relative order It should be maintained Agreement .
Don't use extra space , You must be there. In situ Modify input array And using O ( 1 ) O(1) O(1) Complete with extra space .
In fact, the root of this problem is duplicate removal , Delete the duplicate elements in the array , Keep only one
Example 1:
Example 2:
2. Title diagram
First of all, this question can be used Double pointer Law ;
First , Give Way src Pointing elements , And src-1 ( That is to say src The previous element of ) Compare the elements pointed to , If src The element pointed to is related to src-1 The elements pointed to are equal , that src Just move one bit backwards ;
If it's not equal , Then put src-1 The element pointed to is assigned to dst In the right place , then src and dst At the same time, move one bit backward .
It may sound a little circuitous ? Don't panic. , Follow the picture step by step !
But a little different is , The pointer src Point to the subscript 1 The location of , The pointer dst Or point to the starting position , As shown in the figure 
In limine src Point to the element and src-1 The elements pointed to are 0, in other words src And src-1 The elements pointed to are equal , As shown in the figure 
Then put src Move one back ,dst unchanged , As shown in the figure 
here src The element that points to is 1,src-1 The element that points to is 0, Their values are not equal , Then put src-1 The element pointed to is assigned to dst In the right place ;
then src and dst At the same time, move one bit backward , As shown in the figure 
here src The element that points to is 1,src-1 The element that points to is 1, Their values are equal ;
that src Just move one person back ,dst unchanged , As shown in the figure 
here src The element that points to is 2,src-1 The element that points to is 1, Their values are not equal , Then put src-1 The element pointed to is assigned to dst In the right place ;
then src and dst At the same time, move one bit backward , As shown in the figure 
here src The element that points to is 2,src-1 The element pointed to is also 2, Their values are equal
that src Just move one person back ,dst unchanged , As shown in the figure 
here src The element that points to is 3,src-1 The element that points to is 2, Their values are not equal , Then put src-1 The element pointed to is assigned to dst In the right place ;
then src and dst At the same time, move one bit backward , As shown in the figure 
here src Has pointed to the outside of the array , Then directly assign the last value to dst, then dst Move one more bit backwards
Because you have to put a value , Then we must finish this section , Go to its next position , When the next value is not equal , Will be put there
So the structural design of this problem is as follows 
The essence of this problem is src Follow src-1 The value of the position of , When they are not equal , The previous repeated value interval has been completed , Then put the last reservation in dst The location of .
3. Code implementation
Interface code
int removeDuplicates(int* nums, int numsSize){
int dst = 0;
int src = 1;
while (src < numsSize) {
if (nums[src-1] == nums[src]) {
src++;
}
else {
nums[dst] = nums[src-1];
src++;
dst++;
}
}
nums[dst] = nums[numsSize-1]; // Assign the last value to dst
return ++dst; // dst Move one bit backwards
}
Submit results 
边栏推荐
- [PHP] save session data to redis
- jar文件 反编译(IDEA环境)
- Pads draw 2.54mm row needle
- Save 50% of the cost JD cloud releases a new generation of hybrid CDN products
- 【PHP】常用的header头部定义
- Bug 反馈:同步失败
- DDL, DQL, DML statements
- openstack 虚拟机网卡被重名为cirename0
- Linear algebra Chapter 4 linear equations
- Pycharm加载conda创建pytorch虚拟环境报错,在conda命令行正常
猜你喜欢

cuda11.2对应pytorch安装

【PHP】将 SESSION 数据保存到 Redis

Image preview embedding location of blog maintenance record

测试人员必须知道的软件流程

Training embedded representation of large categories using triple loss and twin neural networks

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘

J3: redis master-slave replication

什么是联邦图机器学习?弗吉尼亚大学最新《联邦图机器学习:概念、技术和应用》综述

Redis6

MySQL 子查询使用方式
随机推荐
Aof & RDB of J2 redis
Bug 反馈:同步失败
首席信息官引导业务变革的指南
查看容器的几种方式
线性代数第3章向量
Redis6
Pyqt5 rapid development and practice 3.6 packaging resource files
服务发现原理分析与源码解读
NLP learning path
Cuda11.2 corresponding pytorch installation
Difficult performance problems solved in those years -- ext4 defragmentation
Still using xshell? You are out. I recommend a more modern terminal connection tool
企业数字化转型成大趋势,选对在线协作工具很重要
Test interview question set UI automated test
Familiarize you with the "phone book" of cloud network: DNS
什么是联邦图机器学习?弗吉尼亚大学最新《联邦图机器学习:概念、技术和应用》综述
The authentication type 10 is not supported
数据库设计三大范式
Pads draw 2.54mm row needle
Reentrantlock learning - Basic Attributes