[LeetCode] Remove Duplicates from Sorted Array II [27]
题目
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].
解题思路
移除数组中反复次数超过2次以上出现的数,可是能够同意反复2次。
这个题类似Remove Duplicates from Sorted Array,第一个想法非常直接就是计数,超过2次的就忽略,根据这个思路的代码见代码一;
上面的思路可行。可是代码看着比較冗余,推断比較多。再来想想原来的数组,该数组是排好序的。假设一个数出现3次以上,那么必有A[i] == A[i-2]。所以依据这个关系能够写出比較精简的代码二。
详见代码。
代码实现
代码一
class Solution {
public:
int removeDuplicates(int A[], int n) {
if(A==NULL || n<=0) return 0;
int start=1, count = 1, back = A[0];
for(int i=1; i<n; ++i){
if(count<2){
A[start++] = A[i];
if(back != A[i]){
back = A[i];
count = 1;
}else{
++count;
}
}else{
if(A[i] != back){
count = 1;
A[start++] = A[i];
back = A[i];
}else{
++count;
}
}
}
return start;
}
};
代码二
class Solution {
public:
int removeDuplicates(int A[], int n) {
if(A==NULL || n<=0) return 0;
if(n==1) return 1;
int start=1,back = A[1];
for(int i=2; i<n; ++i){
if(A[i] != A[i-2]){
A[start++] = back;
back = A[i];
}
}
A[start++] = back;
return start;
}
};
另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Remove Duplicates from Sorted Array II [27]的更多相关文章
- [LeetCode] 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 (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [Leetcode] 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 @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2
class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【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 ...
随机推荐
- Word中如何公式居中标号右对齐
1.鼠标居中 2.插入一行三列表格 3.选中第一个表格,右键-表格属性-单元格-选项:然后回到单元格设置垂直居中,宽度为15%,同理第三个单元格,不过中间单元格也要设置,宽度为70%,这个word没有 ...
- pure-ftp 服务配置篇
FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为 "文传协议" 用于Internet上的控制文件的双向传输. FTP的主要作用,就是让 ...
- vs2015安装VAssistX以后,去除中文注释会有红色下划线方法
---恢复内容开始--- 环境:Visual Studio 2015 问题:代码中出现中文后会带下划线,不舒服-----解决办法. 1.安装完Visual Assist X后会在VS2015的菜单栏出 ...
- 2017.3.31 spring mvc教程(五)Action的单元测试
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- Java之Jackson框架
在Jackson框架中,提供了三种方式用来处理JSON数据: 流式API 在该方式下,使用JsonParser读取JSON数据,使用JsonGenerator写JSON数据.这种方式性能最佳(最低开销 ...
- 倍福TwinCAT(贝福Beckhoff)基础教程 松下伺服驱动器报错 81.0怎么办
同步周期有问题 请确认MOTION的伺服周期是一致的,最好跟MAIN主程序也一样,所有周期都是2ms即可 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.yo ...
- JAVA Eclipse如何开发Android的多页面程序
Fragment可以认为是Activity的一个界面的组成部分,Fragment必须依存于Activity. 在layout文件夹中新建一个xml文件,布局方式采用RelativeLayout,注 ...
- DBCC MEMORYSTATUS
内存管理器 输出的第一节是内存管理器.此部分将显示 SQL Server 的总内存消耗. Memory Manager KB ------------------------------ ------ ...
- 解析Json数据
一.json数据 [{"}] 二.关键代码 public class MainActivity extends Activity { @Override protected void onC ...
- WebGL 启动载入触发更新流程分析
WebGL 启动载入触发更新流程分析 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载 ...