当前位置:网站首页>LeetCode:26. Remove duplicates from an ordered array
LeetCode:26. Remove duplicates from an ordered array
2022-07-06 08:51:00 【Bertil】
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 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 .
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];
}
If all assertions pass , Then your solution will be adopt .
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 :
- 0 <= nums.length <= 3 * 10^4
- -10^4 <= nums[i] <= 10^4
- nums Pressed Ascending array
Their thinking
1. Because the title requires modification in place , So use double pointers . First create a slow pointer i, Point to the first element of the array , Create another fast pointer j, Point to the second element of the array
2. Then traverse the fast pointer j: if nums[j] and nums[i] Unequal , The first will i Increasing 1, And then put nums[i] To change the value of nums[j]
3. Finally, return the new length of the deleted array i+1 that will do
Code
/** * @param {number[]} nums * @return {number} */
var removeDuplicates = function(nums) {
if(!nums.length) return 0;
let i = 0;
for(let j = 1; j < nums.length; j++){
if(nums[j] !== nums[i]){
i++;
nums[i] = nums[j];
}
}
return i + 1;
};
边栏推荐
- Variable length parameter
- Cesium draw points, lines, and faces
- Revit secondary development Hof method calls transaction
- LeetCode:394. String decoding
- marathon-envs项目环境配置(强化学习模仿参考动作)
- 随手记01
- 数学建模2004B题(输电问题)
- Nacos 的安装与服务的注册
- To effectively improve the quality of software products, find a third-party software evaluation organization
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
猜你喜欢
优秀的软件测试人员,都具备这些能力
Light of domestic games destroyed by cracking
C language double pointer -- classic question type
多元聚类分析
vb.net 随窗口改变,缩放控件大小以及保持相对位置
Compétences en mémoire des graphiques UML
Deep anatomy of C language -- C language keywords
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
marathon-envs项目环境配置(强化学习模仿参考动作)
Nacos 的安装与服务的注册
随机推荐
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
Deep analysis of C language data storage in memory
View computer devices in LAN
Navicat premium create MySQL create stored procedure
使用latex导出IEEE文献格式
LeetCode:214. Shortest palindrome string
After reading the programmer's story, I can't help covering my chest...
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
JVM quick start
LeetCode:41. Missing first positive number
Tdengine biweekly selection of community issues | phase III
Simple use of promise in uniapp
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
The mysqlbinlog command uses
LeetCode:387. The first unique character in the string
Revit secondary development Hof method calls transaction
Promise 在uniapp的简单使用
hutool优雅解析URL链接并获取参数
项目连接数据库遇到的问题及解决
Variable length parameter