题目

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. Bluetooth篇 开发实例之八 匹配

    自己写的App匹配蓝牙设备,不需要通过系统设置去连接. 匹配和通信是两回事. 用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK ...

  2. Vue.js 2.0源码解析之前端渲染篇

    一.前言 Vue.js框架是目前比较火的MVVM框架之一,简单易上手的学习曲线,友好的官方文档,配套的构建工具,让Vue.js在2016大放异彩,大有赶超React之势.前不久Vue.js 2.0正式 ...

  3. Makefile中的“-I”(大写i),“-L”(大写l),“-l”(小写l)

    用gcc编译程序时,可能会用到“-I”(大写i),“-L”(大写l),“-l”(小写l)等参数, “-I”(大写i):表示包含头文件: “-L”(大写l):表示库文件目录: “-l”(小写l):表示链 ...

  4. ES里关于对象的拓展

    一.对象类别 在浏览器这样的执行环境中,对象没有统一的标准,在标准中又使用不同的术语描述对象,ES6规范清晰定义了每一个类别的对象,对象的类别如下 1.普通(Ordinary)对象:具有JS对象所有的 ...

  5. ElasticSearch测试数据

    curl命令数据 curl -XPUT http://127.0.0.1:9200/us/user/1 -d "{\"email\":\"john@smith. ...

  6. Hive日期格式转换用法

    如果想把 20180123 转换成 2018-01-23,可以使用: select from_unixtime(unix_timestamp('${p_date}','yyyymmdd'),'yyyy ...

  7. xpath节点匹配简易教程

    2017-03-23 非本人原创,只是写爬虫时很有用,就留下了 <A id="a1"> <B id="b1"> <C id=&qu ...

  8. 用反射来了解Java中泛型的本质

    这篇文章主要通过Class的Method来了解泛型的本质. 先new 两个List,一个不加类型限制,另外一个限制类型为String: ArrayList list = new ArrayList() ...

  9. Jenkins 无法下载插件的解决办法

    有时候在安装插件时可能会出现下图的问题: 这应该是由于天朝的墙导致的,所以笔者就用了手动安装的方式 到https://wiki.jenkins-ci.org/display/JENKINS/Plugi ...

  10. 黑马程序猿——JAVA高新技术——反射

    ----------android培训.java培训.java学习型技术博客.期待与您交流!------------ 一.对于反射的概念 对于JAVA反射机制是在执行状态中,对于随意一个类.都可以知道 ...