当前位置:网站首页>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;
};
边栏推荐
- swagger设置字段required必填
- PC easy to use essential software (used)
- Problems in loading and saving pytorch trained models
- vb.net 随窗口改变,缩放控件大小以及保持相对位置
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
- Computer cleaning, deleted system files
- 如何进行接口测试测?有哪些注意事项?保姆级解读
- MySQL uninstallation and installation methods
- pytorch查看张量占用内存大小
- [sword finger offer] serialized binary tree
猜你喜欢

Image, CV2 read the conversion and size resize change of numpy array of pictures

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 的安装与服务的注册

Deep analysis of C language data storage in memory
![[OC]-<UI入门>--常用控件-UIButton](/img/4d/f5a62671068b26ef43f1101981c7bb.png)
[OC]-<UI入门>--常用控件-UIButton

Screenshot in win10 system, win+prtsc save location

Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges

Mongodb installation and basic operation

vb.net 随窗口改变,缩放控件大小以及保持相对位置

Generator parameters incoming parameters
随机推荐
Revit 二次开发 HOF 方式调用transaction
【剑指offer】序列化二叉树
LeetCode:剑指 Offer 42. 连续子数组的最大和
如何有效地进行自动化测试?
Navicat premium create MySQL create stored procedure
TCP/IP协议
MongoDB 的安装和基本操作
Philosophical enlightenment from single point to distributed
@JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
MYSQL卸载方法与安装方法
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
View computer devices in LAN
Leetcode: Sword finger offer 48 The longest substring without repeated characters
Detailed explanation of heap sorting
TDengine 社区问题双周精选 | 第三期
数学建模2004B题(输电问题)
LeetCode:39. 组合总和
LeetCode:剑指 Offer 04. 二维数组中的查找
[sword finger offer] serialized binary tree