当前位置:网站首页>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;
};
边栏推荐
- [MySQL] database stored procedure and storage function clearance tutorial (full version)
- Deep learning: derivation of shallow neural networks and deep neural networks
- JS pure function
- Esp8266-rtos IOT development
- Deep anatomy of C language -- C language keywords
- MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
- Roguelike game into crack the hardest hit areas, how to break the bureau?
- Beijing invitation media
- sys. argv
- View computer devices in LAN
猜你喜欢
![[embedded] cortex m4f DSP Library](/img/83/ab421d5cc18e907056ec2bdaeb7d5c.png)
[embedded] cortex m4f DSP Library

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

Synchronized solves problems caused by sharing

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

Swagger setting field required is mandatory

优秀的软件测试人员,都具备这些能力

vb. Net changes with the window, scales the size of the control and maintains its relative position

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

【ROS】usb_cam相机标定

visdom可视化实现与检查介绍
随机推荐
C語言雙指針——經典題型
被破解毁掉的国产游戏之光
JS pure function
Current situation and trend of character animation
Precise query of tree tree
2022.02.13 - 238. Maximum number of "balloons"
Visual implementation and inspection of visdom
软件卸载时遇到trying to use is on a network resource that is unavailable
Crash problem of Chrome browser
Computer cleaning, deleted system files
Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
游戏解包的危害及资源加密的重要性
Indentation of tabs and spaces when writing programs for sublime text
Simple use of promise in uniapp
【Nvidia开发板】常见问题集 (不定时更新)
The mysqlbinlog command uses
Problems in loading and saving pytorch trained models
sys. argv
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
JS native implementation shuttle box