当前位置:网站首页>[leetcode] 26. Delete duplicates in the ordered array
[leetcode] 26. Delete duplicates in the ordered array
2022-07-18 06:27:00 【Xiaoqu classmate】
26、 Remove duplicate items from an ordered array
subject :
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 .
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 Of front k individual Element should save the final result .
Insert the final result into nums Of front 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 .
Criteria for judging questions :
The system will use the following code to test your solution :
int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with the correct length
int k = removeDuplicates(nums); // call
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
Example 1:
Input :nums = [1,1,2]
Output :2, nums = [1,2,_]
explain : Function should return the new length 2 , And the original array nums The first two elements of are modified to 1, 2 .
You don't need to think about the elements in the array that follow the new length .
Example 2:
Input :nums = [0,0,1,1,1,2,2,3,3,4]
Output :5, nums = [0,1,2,3,4]
explain : Function should return the new length 5 , And the original array nums The first five elements of are modified to 0, 1, 2, 3, 4 .
You don't need to think about the elements in the array that follow the new length .
Tips :
1 <= nums.length <= 3 * 10 ^4
-10 ^4 <= nums[i] <= 10 ^4
nums Pressed Ascending array
Their thinking :
- Due to the given array
numsIs ordered , So for anyi<j, Ifnums[i]=nums[j], For anyi≤k≤j, There must benums[i]=nums[k]=nums[j], That is, the subscripts of equal elements in the array must be continuous . Using the characteristics of array order , Duplicate elements can be deleted by double pointer method . - If the array
numsThe length of is 0, Then the array does not contain any elements , Therefore return 0. - Array
numsIs longer than 0 when , The array contains at least one element , At least one element remains after deleting the duplicate element , thereforenums[0]Just keep it as it is , From the subscript 1 Start removing duplicate elements . - Define two pointers
fastandslowRespectively Fast pointer and slow pointer , The fast pointer indicates the subscript position reached by traversing the array , The slow pointer indicates the subscript position to be filled in by the next different element , Initially, both pointers point to the subscript 1. - Hypothetical array
numsThe length of is n. Fast pointerfastGo through from1 To n−1Each location of , For each location , Ifnums[fast] !=nums[fast−1], explainnums[fast]Different from the previous elements , So it willnums[fast]Copy the value of tonums[slow],And thenslowThe value of the add 1, That is to point to the next position .
After the traversal , fromnums[0]Tonums[slow−1] OfEach element is different and contains each different element in the original array , So the new length isslow, returnslowthat will do .
Reference code :
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n == 0) {
return 0;
}
int fast = 1, slow = 1;
while (fast < n) {
if (nums[fast] != nums[fast - 1]) {
nums[slow] = nums[fast];
++slow;
}
++fast;
}
return slow;
}
}

边栏推荐
- 【系统设计】4S分析法
- CS5801_ HDMI to EDP advantage replaces lt6711a solution
- [server data recovery] a data recovery case of RAID5 crash caused by hard disk offline during data synchronization of a hot spare disk of an IBM model
- 软件研发效能需求价值流分析专题
- Practice of online problem feedback module (II): automatic generation of class file by encapsulating code
- Jerry's use of one driven two burner burning notes [chapter]
- R language dplyr package summary_ The all function calculates the mean and median of all numerical data columns in the dataframe data, and filters the numerical data columns with happy (summarize all
- 网络套接字编程
- Comment définir Notepad + + comme mode d'ouverture par défaut
- 如何在企业工作中应用知识管理,解决企业的问题?
猜你喜欢

Function advanced application

Comment définir Notepad + + comme mode d'ouverture par défaut

canal实现从mysql实时同步数据到es

After reading these five reasons, I finally know the reason why FTP was replaced

如何在企业工作中应用知识管理,解决企业的问题?

Database daily question --- day 23: game play analysis L

迭代器与生成器

MQ系列2:消息中间件的技术选型

杰理之调式时要修改 INI 文件配置【篇】

The difference between let / const /var
随机推荐
The INI file configuration should be modified during Jerry's mode [chapter]
Together with Alibaba cloud, grafana labs will provide the first grafana hosting service in China
正则表达式
ECCV 2022 | 多域长尾分布学习,不平衡域泛化问题研究(开源)
Bisect module
Animation and encapsulation (offset, client, scroll series)
Diwen serial port screen tutorial (2)
杰理之内置触摸可供修改的参数【篇】
@RequestBody
MYSQL建表语句错误:1103-Incorrect table name
Huawei's general card identification function enables multiple card bindings with one key
@EqualsAndHashCode注解的使用
MySQL数据库在触发器中定义游标
997. Find the judge of the town
@Use of equalsandhashcode annotation
Face the object
牛啊!2小时复现顶会论文,他的秘诀是——
“沉浸式”住宿体验——酒店的新瓶,民宿的老酒
NVIDA CUDA-DirverAPI入门
Matplotlib绘图报错:“! LaTeX Error: File `type1cm.sty‘ not found.“ 解决办法