【Leetcode】【Medium】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3]
,
Your function should return length = 5
, and A is now [1,1,2,2,3]
.
解题思路1:
遍历A,i表示当前遍历到的数组下标,length表示当前已重新填充的数组下标;设置一个flag用于记录是否已经出现重复;
从i=length=1开始(数组首元素一定不用移除),当A[i] = A[i-1],如果flag为true,则说明A[i-1]=A[i-2],因此A[i]需要移除(不对数组做更新操作);
当A[i] = A[i-1],如果flag为false,则说明A[i-1]!=A[i-2],因此在当前length位置处使A[length] = A[i],length++,flag不变;
当A[i] != A[i-1],A[length] = A[i],length++,flag = false;
最终length指向新数组最后一个元素的下一个位置,因此数值刚好等于新数组个数,直接返回;
解题思路2:
由于代码中有大量的if else 判断语句,非常难看,因此突然想到,直接判断A[i] 是否等于 A[i-2]就可以了,这样可以直接判断出是否连续三个及以上重复,如果重复了3个以上,就不添加到新数组队列中;
但是,由于从i=length=2开始,判断if (A[i] == A[i-2]),A[i-2]有可能已经是新数组的元素,即有可能已经更新的元素值,原数组被破坏了,就无法正确判断是否连续3个;
对于这种向前判断无法成功的例子,往往可以尝试向后判断,即从i=length=0开始,判断if (A[i] == A[i+2]);
至此,问题很轻松的解决了,而且代码行数非常短,由于判断if (A[i] == A[i+2]) 相当于已经判断了最后三个元素的有效性,因此最后的两个元素一定是合法的,不用移除;
代码:
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n <= )
return n;
int length = ; for (int i = ; i < n - ; ++i) {
if (A[i] != A[i+])
A[length++] = A[i];
} A[length++] = A[n - ];
A[length++] = A[n - ];
return length;
}
};
【Leetcode】【Medium】Remove Duplicates from Sorted Array II的更多相关文章
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 【随笔】设置title标题图标为自定义图片
第一步: 利用图标工具(有很多)制作图标文件(favicon.ico)上传到网站所在的服务器的根目录下,这个文件必须是16*16大小的图标文件. 第二步: 在<head></head ...
- 使用idea开发工具,nginx服务部署Extjs6,SpringBoot项目到服务器
编译ExtJs文件 1.输入命令 2.开始编译 3.找到编译后的文件 E:\idea_project\BaiSheng_Model\fin-ui\build\production\Admin 4.将文 ...
- Java入门系列-18-抽象类和接口
抽象类 在第16节继承中,有父类 People People people=new People(); people.sayHi(); 实例化People是没有意义的,因为"人"是 ...
- [转载+补充]windows下SVN客户端的安装
来源:TortoiseSVN新人使用指南 1. 首先安装SVN客户端,windows一般选择乌龟客户端https://tortoisesvn.net/downloads.html. 2. 根据系统位数 ...
- 深入理解JavaScript系列(42):设计模式之原型模式
介绍 原型模式(prototype)是指用原型实例指向创建对象的种类,并且通过拷贝这些原型创建新的对象. 正文 对于原型模式,我们可以利用JavaScript特有的原型继承特性去创建对象的方式,也就是 ...
- [转]How to Use Web API OData to Build an OData V4 Service without Entity Framework
本文转自:http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity- ...
- css内容简介(层叠样式表)
css是对网页编辑的加色,是对其功能的渲染. 根据规范每个元素都有一个display属性,每个元素都有一个------------如div元素他的默认为block. 行内元素和块级元素 块级元素会占据 ...
- 微信小程序wx:for循环
最近做微信小程序碰到了一些问题,和wx:for循环相关,wx:for有很多用途,例如可以用于swiper中图片的循环,也就是所谓的轮播图,也可以用于其它的循环,可以大大地减少代码量. 但wx:for. ...
- 微软的TransactionScope类是个好玩意
最近发现微软自带的TransactionScope(.Net Framework 2之后)是个好东东,提供的功能也很强大. 首先说说TransactionScope是什么,并能为我们做什么事情.其实看 ...
- Oracle Spatial GIS相关研究
1.Oracle Spatial 概念相关 Oracle Spatial 是Oracle 数据库强大的核心特性,包含了用于存储矢量数据类型.栅格数据类型和持续拓扑数据类型的原生数据类型.Oracle ...