当前位置:网站首页>LeetCode:26. 删除有序数组中的重复项
LeetCode:26. 删除有序数组中的重复项
2022-07-06 08:44:00 【Bertil】
给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。
由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应该保存最终结果。
将最终结果插入 nums 的前 k 个位置后返回 k 。
不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
判题标准:
系统会用下面的代码来测试你的题解:
int[] nums = [...]; // 输入数组
int[] expectedNums = [...]; // 长度正确的期望答案
int k = removeDuplicates(nums); // 调用
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
如果所有断言都通过,那么您的题解将被 通过。
示例 1:
输入:nums = [1,1,2]
输出:2, nums = [1,2,_]
解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。
示例 2:
输入:nums = [0,0,1,1,1,2,2,3,3,4]
输出:5, nums = [0,1,2,3,4]
解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。
提示:
- 0 <= nums.length <= 3 * 10^4
- -10^4 <= nums[i] <= 10^4
- nums 已按 升序 排列
解题思路
1.因为题目要求原地修改,所以使用双指针。首先创建一个慢指针i,指向数组第一个元素,再创建一个快指针j,指向数组第二个元素
2.然后遍历快指针j:若nums[j]和nums[i]不等,则先将i递增1,然后把nums[i]的值改为nums[j]
3.最后返回删除后数组的新长度i+1即可
代码
/** * @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;
};
边栏推荐
- After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
- JVM performance tuning and practical basic theory - Part 1
- Mobile phones and computers on the same LAN access each other, IIS settings
- What is CSRF (Cross Site Request Forgery)?
- Roguelike游戏成破解重灾区,如何破局?
- 软件卸载时遇到trying to use is on a network resource that is unavailable
- JVM 快速入门
- Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
- 软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
- ROS compilation calls the third-party dynamic library (xxx.so)
猜你喜欢

sublime text的编写程序时的Tab和空格缩进问题

egg. JS project deployment online server

Detailed explanation of heap sorting

pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof

C语言双指针——经典题型

【ROS】usb_ Cam camera calibration

swagger设置字段required必填

UnsupportedOperationException异常

Double pointeur en langage C - - modèle classique

Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
随机推荐
Bottom up - physical layer
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
Sublime text using ctrl+b to run another program without closing other runs
[MySQL] lock
China polyether amine Market Forecast and investment strategy report (2022 Edition)
vb. Net changes with the window, scales the size of the control and maintains its relative position
Esp8266-rtos IOT development
C語言雙指針——經典題型
TCP/IP协议
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
Crash problem of Chrome browser
Variable length parameter
PC easy to use essential software (used)
Charging interface docking tutorial of enterprise and micro service provider platform
China vanadium battery Market Research and future prospects report (2022 Edition)
Deep analysis of C language pointer
[MySQL] log
按位逻辑运算符
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