[LeetCode] 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] Remove Duplicates from Sorted Array 有序数组中去除重复项的更多相关文章
- [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] 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每日一题]80. 删除有序数组中的重复项 II
[LeetCode每日一题]80. 删除有序数组中的重复项 II 问题 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度. 不要使用额外 ...
- [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 ...
- [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 ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- 083 Remove Duplicates from Sorted List 有序链表中删除重复的结点
给定一个排序链表,删除所有重复的元素使得每个元素只留下一个.案例:给定 1->1->2,返回 1->2给定 1->1->2->3->3,返回 1->2- ...
- LeetCode(26): 删除排序数组中的重复项
Easy! 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...
随机推荐
- 【分布式】Zookeeper请求处理
一.前言 在前面学习了Zookeeper中服务器的三种角色及其之间的通信,接着学习对于客户端的一次请求,Zookeeper是如何进行处理的. 二.请求处理 2.1 会话创建请求 Zookeeper服务 ...
- 2015游戏蛮牛——蛮牛杯第四届开发者大赛 创见VR未来开启报名
蛮牛杯启动了,大家开始报名! http://cup.manew.com/ 这不是一篇普通的通稿,别着急忽略它.它是一篇可以让你梦想变现的通稿! 从某一天开始,游戏蛮牛就立志要为开发者服务,我们深知这一 ...
- 深入理解定时器系列第一篇——理解setTimeout和setInterval
× 目录 [1]setTimeout [2]setInterval [3]运行机制[4]作用[5]应用 前面的话 很长时间以来,定时器一直是javascript动画的核心技术.但是,关于定时器,人们通 ...
- oracle运算符
单引号('): 在Oracle中,应该只运用单引号将文本和字符和日期括起来,不能运用引号(包括单双引号)将数字括起来. 双引号("): 在Oracle中,单双引号意思不同.双引号被用来将包含 ...
- Visual Studio (VSIX,项目模板 )制作
下载Vsiual Studio 2012 SDK 下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=30668 提示:一定要注意 ...
- 使用 GitHub 和 Hexo 搭建个人独立博客
Wordpress这类博客系统功能强大,可对与我只想划拉的写点东西的人,感觉大材小用了.而且wp需要部署,网站的服务器也会带来问题,国内的服务器首先需要备案,费用不低:国外服务器访问速度受影响.近 ...
- 同步辅助类CountDownLatch用法
CountDownLatch是一个同步辅助类,犹如倒计时计数器,创建对象时通过构造方法设置初始值,调用CountDownLatch对象的await()方法则使当前线程处于等待状态,调用countDow ...
- spring和struts2的整合的xml代码
导入spring的pring-framework-4.0.4.RELEASE的所有包,导入struts2下(对于初学的推荐)bin下所有的包,虽然有些包可以能现在你用不到,但可以保证你基本上不会出现缺 ...
- css3制作炫酷导航栏效果
今天主要利用hover选择器.鼠标滑过查看效果. 一.普通导航栏 Home Content Service Team Contact 对于这种普通的导航栏,只是鼠标滑过的时候颜色会变,所以思路变得很简 ...
- AJAX实现简单的注册页面异步请求
p { margin: 0px; padding: 0px } AJAX简介 (1)AJAX = 异步 JavaScript 和 XML. (2)AJAX 是一种用于创建快速动态网页的技术. (3)通 ...