当前位置:网站首页>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;
};
边栏推荐
- Variable length parameter
- 生成器参数传入参数
- 电脑清理,删除的系统文件
- JVM 快速入门
- 【ROS】usb_ Cam camera calibration
- China vanadium battery Market Research and future prospects report (2022 Edition)
- Swagger setting field required is mandatory
- Sublime text using ctrl+b to run another program without closing other runs
- PLT in Matplotlib tight_ layout()
- 优秀的软件测试人员,都具备这些能力
猜你喜欢

sublime text中conda环境中plt.show无法弹出显示图片的问题

可变长参数

生成器参数传入参数

Process of obtaining the electronic version of academic qualifications of xuexin.com

电脑清理,删除的系统文件
![[embedded] print log using JLINK RTT](/img/22/c37f6e0f3fb76bab48a9a5a3bb3fe5.png)
[embedded] print log using JLINK RTT

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

egg. JS project deployment online server

Indentation of tabs and spaces when writing programs for sublime text

JVM performance tuning and practical basic theory - Part 1
随机推荐
Navicat premium create MySQL create stored procedure
Sort according to a number in a string in a column of CSV file
Trying to use is on a network resource that is unavailable
Delay initialization and sealing classes
pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
Purpose of computer F1-F12
堆排序详解
marathon-envs项目环境配置(强化学习模仿参考动作)
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
JS native implementation shuttle box
Promise 在uniapp的简单使用
Synchronized solves problems caused by sharing
China's high purity aluminum target market status and investment forecast report (2022 Edition)
egg. JS getting started navigation: installation, use and learning
swagger设置字段required必填
2022.02.13 - NC002. sort
ROS编译 调用第三方动态库(xxx.so)
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
Problems in loading and saving pytorch trained models
电脑F1-F12用途