当前位置:网站首页>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;
};
边栏推荐
- Chrome浏览器的crash问题
- Pointer advanced --- pointer array, array pointer
- China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
- 有效提高软件产品质量,就找第三方软件测评机构
- Bitwise logical operator
- ROS compilation calls the third-party dynamic library (xxx.so)
- After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
- 游戏解包的危害及资源加密的重要性
- 角色动画(Character Animation)的现状与趋势
- JVM performance tuning and practical basic theory - Part 1
猜你喜欢

延迟初始化和密封类

After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF

Roguelike game into crack the hardest hit areas, how to break the bureau?

visdom可视化实现与检查介绍

【嵌入式】使用JLINK RTT打印log

被破解毁掉的国产游戏之光

Screenshot in win10 system, win+prtsc save location

Unified ordering background interface product description Chinese garbled

JVM performance tuning and practical basic theory - Part 1

游戏解包的危害及资源加密的重要性
随机推荐
Crash problem of Chrome browser
Function coritization
MySQL learning record 10getting started with JDBC
Double pointeur en langage C - - modèle classique
poi追加写EXCEL文件
软件卸载时遇到trying to use is on a network resource that is unavailable
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
hutool优雅解析URL链接并获取参数
The harm of game unpacking and the importance of resource encryption
The mysqlbinlog command uses
marathon-envs项目环境配置(强化学习模仿参考动作)
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
堆排序详解
Trying to use is on a network resource that is unavailable
Current situation and trend of character animation
Swagger setting field required is mandatory
Sublime text using ctrl+b to run another program without closing other runs
Deep analysis of C language data storage in memory
Indentation of tabs and spaces when writing programs for sublime text
Image,cv2读取图片的numpy数组的转换和尺寸resize变化