题目

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. 服务器不安装Excel,实现导出Excel功能

    /// <summary> /// 导出为Excel /// </summary> /// <param name="sender"></ ...

  2. MSSQL收缩事务日志&日志文件过大无法收缩

    原文:MSSQL收缩事务日志&日志文件过大无法收缩 一.MS SQL SERVER 2005 --1.清空日志 exec('DUMP TRANSACTION 数据库名 WITH NO_LOG' ...

  3. debug with Linux slub allocator

     http://thinkiii.blogspot.jp/2014/02/debug-with-slub-allocator.html The slub allocator in Linux has ...

  4. linux内存查看方法

    cat /proc/meminfo 查看RAM使用情况,最简单的方法是通过/proc/meminfo.这个动态更新的虚拟文件实际上是许多其他内存相关工具(如:free / ps / top)等的组合显 ...

  5. @Component-@Resource-@Repository-@Service-@Controller的区别和理解-------springMVC

    1.作用: @Component------------------------泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注.(Component-------成分; 组分; 零件) ...

  6. python langid实现语种识别

    2017-04-26 语料数据入库时有个小需求,需要用一个字段存储语料的语种,偶然发现langid可以实现这一功能,再次感叹python的好用! #coding=utf-8 import langid ...

  7. fabricjs 高级篇(自定义类型)

    原文:https://www.sitepoint.com/fabric-js-advanced/ <html> <head> <script src='./js/fabr ...

  8. 转: 使用 Velocity 模板引擎快速生成代码

    from:https://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ 评注: 1. velocity 的基本语法 2. 生成代码的用法.

  9. javascript 回车实现 tab 切换功能完美解决

    最经有一个项目是给化工厂做的在使用的过程中需要输入大量的数据,使用的都是小键盘区,在以前都是通过excel录入数据的现在, 在网页上需要实现excel 那样的回车换行的功能在网上找了有关这方面的问题但 ...

  10. .net 真实代理和透明代理的交互

    .本地代理调用 using System; using System.Runtime.Remoting ; using System.Runtime.Remoting.Services ; using ...