当前位置:网站首页>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;
};
边栏推荐
- Navicat premium create MySQL create stored procedure
- [embedded] print log using JLINK RTT
- SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
- sublime text中conda环境中plt.show无法弹出显示图片的问题
- View computer devices in LAN
- Pytorch view tensor memory size
- C語言雙指針——經典題型
- 【ROS】usb_ Cam camera calibration
- Light of domestic games destroyed by cracking
- To effectively improve the quality of software products, find a third-party software evaluation organization
猜你喜欢

ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法

MYSQL卸载方法与安装方法

Deep analysis of C language data storage in memory

Alibaba cloud server mining virus solution (practiced)

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

Image,cv2读取图片的numpy数组的转换和尺寸resize变化

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

After reading the programmer's story, I can't help covering my chest...

LeetCode:221. 最大正方形

Warning in install. packages : package ‘RGtk2’ is not available for this version of R
随机推荐
Shift Operators
LeetCode:673. Number of longest increasing subsequences
Variable length parameter
LeetCode:387. The first unique character in the string
704 binary search
marathon-envs项目环境配置(强化学习模仿参考动作)
[NVIDIA development board] FAQ (updated from time to time)
CSP first week of question brushing
Promise 在uniapp的简单使用
【嵌入式】Cortex M4F DSP库
Philosophical enlightenment from single point to distributed
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
opencv+dlib实现给蒙娜丽莎“配”眼镜
Export IEEE document format using latex
Simple use of promise in uniapp
Mobile phones and computers on the same LAN access each other, IIS settings
LeetCode:41. 缺失的第一个正数
Problems in loading and saving pytorch trained models
POI add write excel file
力扣每日一题(二)