当前位置:网站首页>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;
};
边栏推荐
- MongoDB 的安装和基本操作
- Simple use of promise in uniapp
- Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
- Navicat premium create MySQL create stored procedure
- To effectively improve the quality of software products, find a third-party software evaluation organization
- Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
- How to conduct interface test? What are the precautions? Nanny level interpretation
- SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
- 如何正确截取字符串(例:应用报错信息截取入库操作)
- UML diagram memory skills
猜你喜欢
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
Restful API design specification
[sword finger offer] serialized binary tree
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
Detailed explanation of dynamic planning
[embedded] print log using JLINK RTT
JVM quick start
Sublime text using ctrl+b to run another program without closing other runs
随机推荐
Leetcode: Sword finger offer 48 The longest substring without repeated characters
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
Crash problem of Chrome browser
Super efficient! The secret of swagger Yapi
优秀的软件测试人员,都具备这些能力
数学建模2004B题(输电问题)
LeetCode:39. Combined sum
LeetCode:221. Largest Square
Sublime text using ctrl+b to run another program without closing other runs
Revit secondary development Hof method calls transaction
hutool优雅解析URL链接并获取参数
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
LeetCode:394. 字符串解码
[sword finger offer] serialized binary tree
Generator parameters incoming parameters
Navicat Premium 创建MySql 创建存储过程
[OC]-<UI入门>--常用控件-UIButton
Compétences en mémoire des graphiques UML