题目

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. linux之ls指令的关键字过滤显示

    假设~/admin的文件夹下面有几个文件: a.pm b.pm c.pl d.pl e.pml 想只显示.pm结尾的可以用: ls *.pm 或者 ls | grep .pm,两者等价. 想显示b.开 ...

  2. ArcMAP定义投影坐标

    WGS84形式的高斯克吕格投影在ArcGIS系统中是不存在的,需要自己去定义.下面为MARK的定义过程.

  3. mailkit----163邮箱登录拉取邮件的坑

    在使用mailkit的ImapClient拉取邮件的时候,如果我们使用的是网易的邮箱(如:163.126等),如果你没有按照网易的设置去打开IMAP协议,那么将无法登录邮箱,并且发送一封使用不安全的客 ...

  4. easyui numberbox precision属性

    //设置easyui numbox 最小值为0,保留2为小数 <input id="payPrice" type="text" name="pa ...

  5. python 注释xml的元素

    use xml.dom.minidom 注释xml元素和去除xml注释. code is: #!/usr/bin/env python from xml.dom import minidom impo ...

  6. Java学习之泛型和异常

    泛型 1,设计原则或目的:只要代码在编译的时候没有错误,就不会抛异常.  2,泛型通配符  :类型通配符一般是使用 ? 代替具体的类型实参.注意了,此处是类型实参,而不是类型形参!相当于(父类作用)L ...

  7. asp.net显示评论的时候为几天前的格式

    自己做的一个小项目实现的功能,做个记录先~ 效果如图: 代码如下: public static class TimerHelper { public static string GetTimeSpan ...

  8. 2017.4.10 spring-ldap官方文档学习

    官网:http://www.springframework.org/ldap 官方文档及例子(重要):http://docs.spring.io/spring-ldap/docs/2.1.0.RELE ...

  9. dubbo官网和帮助文档

    dubbo官网和帮助文档 https://github.com/apache/incubator-dubbo 内含帮助文档: http://dubbo.apache.org/books/dubbo-d ...

  10. 《Qt on Android核心编程》相关资源

    有不少朋友反馈在搭建 Qt on Android 开发环境时遇到了问题,诸如 Android SDK 无法下载. jdk 找不到合适的版本号.创建 AVD 出错等等.为此我把与<Qt on An ...