题目

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;
}
};
假设你认为本篇对你有收获,请帮顶。

另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.

你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/29822565
)

[LeetCode] Remove Duplicates from Sorted Array II [27]的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. [Leetcode] Remove Duplicates From Sorted Array II (C++)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  3. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  5. LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...

  6. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  7. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

随机推荐

  1. Nand 的几个名词:oob,bbt,ecc

    转:http://blog.csdn.net/lanmanck/article/details/4230904 例如Samsung K9F1208U0B,数据存储容量为64MB,采用块页式存储管理.8 ...

  2. kubernetes1.5.2--部署监控服务

    本文基于kubernetes 1.5.2版本编写 Heapster是kubernetes集群监控工具.在1.2的时候,kubernetes的监控需要在node节点上运行cAdvisor作为agent收 ...

  3. 管理 node 版本,选择 nvm 还是 n?

    来源:http://taobaofed.org/blog/2015/11/17/nvm-or-n/ 引子 我本机安装着 nvm,而 node 本来一直运行在 0.x 的老版本上.后来为了跑 ES6,我 ...

  4. cordova百度导航插件使用

    org.ssgroup.sope.cordova.baiduNavi 插件已经开源至 https://github.com/shenshouer/org.ssgroup.sope.cordova.ba ...

  5. 安装php扩展模块参数memcache和memcached在php中的应用

    一, memcache和memcached的区别与关系统php要想去访问memcached就得需要memcache扩展,这个道理和php连接mysql一样. 你不安装memcache扩展就没法识别me ...

  6. 对类中的成员函数的定义和声明最后添加一个const是什么意思?

    1.const修饰的成员函数只能调用const修饰的成员函数,且不能修改数据成员变量的值. 2.const修饰的类对象只能调用const修饰的成员函数. 3.const修饰的类对象可以调用非const ...

  7. 转: WebView载入一个网页 但是退出对应的activity时, 声音、视频无法停止播放 解决方案(未验证)

    1. webview.onPause 2. webview独立进程,杀进程3.小场景可以不用这么复杂有个技巧就是在activity退出的时候加载一个空白页面,就能解决

  8. Elasticsearch教程(六) elasticsearch Client创建

    Elasticsearch  创建Client有几种方式. 首先在 Elasticsearch  的配置文件 elasticsearch.yml中.定义cluster.name.如下: cluster ...

  9. 怎样设置gephi可画大规模网络图形

    (1)编辑gephi.conf 文件夹:\etc\gephi.conf 默认512MB.你能够改为22GB,约22528M # ${HOME} will be replaced by user hom ...

  10. 使用json-server搭建模拟api接口

    转载:http://blog.csdn.net/adojayfan/article/details/55011674 作为前端和客户端开发人员,在后端还没有给出对应的api接口时,我们无法做测试. 这 ...