当前位置:网站首页>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;
};
边栏推荐
- Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
- Notes 01
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- ESP8266-RTOS物联网开发
- Promise 在uniapp的简单使用
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
- [MySQL] multi table query
- pytorch查看张量占用内存大小
- Hutool gracefully parses URL links and obtains parameters
- LeetCode:836. Rectangle overlap
猜你喜欢
UnsupportedOperationException异常
【ROS】usb_ Cam camera calibration
TP-LINK 企业路由器 PPTP 配置
[embedded] print log using JLINK RTT
LeetCode:124. 二叉树中的最大路径和
C语言双指针——经典题型
Image, CV2 read the conversion and size resize change of numpy array of pictures
角色动画(Character Animation)的现状与趋势
Navicat premium create MySQL create stored procedure
TCP/IP协议
随机推荐
Crash problem of Chrome browser
Computer graduation design PHP Zhiduo online learning platform
Indentation of tabs and spaces when writing programs for sublime text
LeetCode:剑指 Offer 04. 二维数组中的查找
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Nacos 的安装与服务的注册
项目连接数据库遇到的问题及解决
swagger设置字段required必填
如何有效地进行自动化测试?
Mobile phones and computers on the same LAN access each other, IIS settings
LeetCode:41. Missing first positive number
Excellent software testers have these abilities
LeetCode:236. 二叉树的最近公共祖先
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
Trying to use is on a network resource that is unavailable
如何正确截取字符串(例:应用报错信息截取入库操作)
TP-LINK 企业路由器 PPTP 配置
JVM quick start
UML圖記憶技巧