当前位置:网站首页>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;
};
边栏推荐
- PC easy to use essential software (used)
- logback1.3. X configuration details and Practice
- vb.net 随窗口改变,缩放控件大小以及保持相对位置
- JVM performance tuning and practical basic theory - Part 1
- Unsupported operation exception
- Roguelike game into crack the hardest hit areas, how to break the bureau?
- What is CSRF (Cross Site Request Forgery)?
- 2022.02.13 - NC003. Design LRU cache structure
- 堆排序详解
- Revit 二次开发 HOF 方式调用transaction
猜你喜欢
软件卸载时遇到trying to use is on a network resource that is unavailable
egg. JS getting started navigation: installation, use and learning
个人电脑好用必备软件(使用过)
Synchronized solves problems caused by sharing
Unified ordering background interface product description Chinese garbled
vb.net 随窗口改变,缩放控件大小以及保持相对位置
深度剖析C语言数据在内存中的存储
2022.02.13 - 238. Maximum number of "balloons"
Simple use of promise in uniapp
ESP8266-RTOS物联网开发
随机推荐
Bitwise logical operator
Promise 在uniapp的简单使用
【Nvidia开发板】常见问题集 (不定时更新)
The harm of game unpacking and the importance of resource encryption
Trying to use is on a network resource that is unavailable
C语言深度解剖——C语言关键字
logback1.3. X configuration details and Practice
JS pure function
软件卸载时遇到trying to use is on a network resource that is unavailable
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
Precise query of tree tree
Deep analysis of C language data storage in memory
生成器参数传入参数
Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
如何进行接口测试测?有哪些注意事项?保姆级解读
On the inverse order problem of 01 knapsack problem in one-dimensional state
电脑清理,删除的系统文件
Light of domestic games destroyed by cracking
[embedded] cortex m4f DSP Library
China polyether amine Market Forecast and investment strategy report (2022 Edition)