[LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,2], Your function should return length =2
, with the first two elements ofnums
being1
and2
respectively. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4], Your function should return length =5
, with the first five elements ofnums
being modified to0
,1
,2
,3
, and4
respectively. It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
这道题要我们从有序数组中去除重复项,和之前那道 Remove Duplicates from Sorted List 的题很类似,但是要简单一些,因为毕竟数组的值可以通过下标直接访问,而链表不行。那么这道题的解题思路是使用快慢指针来记录遍历的坐标,最开始时两个指针都指向第一个数字,如果两个指针指的数字相同,则快指针向前走一步,如果不同,则两个指针都向前走一步,这样当快指针走完整个数组后,慢指针当前的坐标加1就是数组中不同数字的个数,代码如下:
解法一:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int pre = , cur = , n = nums.size();
while (cur < n) {
if (nums[pre] == nums[cur]) ++cur;
else nums[++pre] = nums[cur++];
}
return nums.empty() ? : (pre + );
}
};
我们也可以用 for 循环来写,这里的j就是上面解法中的 pre,i就是 cur,所以本质上都是一样的,参见代码如下:
解法二:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int j = , n = nums.size();
for (int i = ; i < n; ++i) {
if (nums[i] != nums[j]) nums[++j] = nums[i];
}
return nums.empty() ? : (j + );
}
};
这里也可以换一种写法,用变量i表示当前覆盖到到位置,由于不能有重复数字,则只需要用当前数字 num 跟上一个覆盖到到数字 nums[i-1] 做个比较,只要 num 大,则一定不会有重复(前提是数组必须有序),参见代码如下:
解法三:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int i = ;
for (int num : nums) {
if (i < || num > nums[i - ]) {
nums[i++] = num;
}
}
return i;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/26
类似题目:
Remove Duplicates from Sorted List
Remove Duplicates from Sorted List II
Remove Duplicates from Sorted Array II
类似题目:
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
随机推荐
- 数据持久化之Data Volume
废话不多说直接操作 1.启动一个MySQL测试容器 [root@localhost labs]# docker pull mysql #下载MySQL镜像 [root@localhost labs]# ...
- Filebeat和pipleline processor-不部署logstash,实现对数据的处理
利用ingest node所提供的Pipeline帮我们对数据进行处理. 在Elasticsearch中的配置文件elasticsearch.yml文件中配置:node.ingest: true in ...
- NET 特性(Attribute)
NET 特性(Attribute) 转自 博客园(Fish) 特性(Attribute):是用于在运行时传递程序中各种元素(比如类.方法.结构.枚举.组件等)的行为信息的声明性标签. 您可以通过使用特 ...
- C++调用linux命令并获取返回值
qt中封装了相关的方法, 但是因为我的命令中用到了管道命令, 出现了非预期结果, 所有改用了linux系统原生的方法. 下边是一个判断某进程是否存在的例子. 当前存在一个问题,当linux返回多行时, ...
- 前端之:js
JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECM ...
- 快速上手Mac效率神器Alfred以及Alfred常用操作
前言 Alfred,想必大家就算没用过也耳闻过.Alfred是一个让你可以丢掉鼠标的神器.很多读者可能之前认为Alfred的学习成本高,或者感觉它太复杂,而望之却步.其实Alfred并非高不可攀,本文 ...
- IOS疯狂基础之观察者模式
转自:http://blog.csdn.net/wudizhukk/article/details/8981535 一.KVO Key-Value Observing,它提供一种机制,当指定的对象的属 ...
- Qt压缩和解压 zip
zlib编译详见 https://blog.csdn.net/zhangxuechao_/article/details/85049711 下载quazip https://github.com/st ...
- access truncate
access int access(const char *pathname, int mode); 确定文件或文件夹的访问权限 //unistd.h #define R_OK 4 /* Test f ...
- 个人项目wc(Java)
个人项目(Java) 一丶Github地址:https://github.com/SAH2019/S ...