【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 ...
随机推荐
- Math.round、Math.floor、Math.ceil 区别
1.Math.round() 按照四舍五入的方式返回值 例如:Math.round(9.5)=10 Math.round(9.4)=9 2.Math.floor()返回最小整数 例如:Math. ...
- Java 继承学习
Java 继承 继承实现: 在Java中,如果实现继承的,需要使用Java关键字——extends : 被继承的类叫做超类,继承超类的类叫子类.(一个子类亦可以是另一个类的超类) class 子类 e ...
- Java_方法的调用②及案例
语法格式: 方法名称([参数列表]); //注意:只能调用本类的方法 案例: class Method01{ public static void print(){ for(int i = 1; i ...
- HDU 5656 ——CA Loves GCD——————【dp】
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- linux 查看服务器系统资源和负载,以及性能监控
1.查看磁盘 df -h 2.查看内存大小 free [-m|g]#按MB,GB显示内存 3.查看每个进程的情况 cat /proc/5346/status PID 4.查看负载 uptime 5.查 ...
- Echarts 修改折线的颜色和折线的点的大小方法
series: [{ type: 'line', smooth:true,//折点是圆弧状的 showSymbol: true, ...
- django常用封装
#encoding:utf-8from django.shortcuts import render_to_responseimport hashlibfrom binascii import b2a ...
- Java基础(十二)IO输入输出
一.IO 概述 1.IO 概念 IO:I 代表 Input 输入:O 代表 Output 输出. Java 中 IO 是以流为基础进行输入输出,所有的数据被串行化(保存)写入输出流,或者从输入流读入. ...
- Spring cloud ReadTimeout 问题解决
今天使用Spring cloud @FeignClient 调用远程服务的时候,出现readTimeout问题,通过找资料解决方式如下 在Spring.properties 配置文件中添加如下属性解决 ...
- Linux服务器安装tomcat、JDK、SVN等常用开发软件总结
本来本文发布到首页的,该网站运营人员移除了,说我这篇博文太简单了,如果感觉我这篇博文有用的,大家给个推荐,打一下运营人员的脸 目录 一.Ubuntu 16.04下安装JDK(spring 3.2不支持 ...