题目:

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].

Tag:

Array; Two Pointers

体会:

继续是quicksort中partition函数的改写。和Remove Duplicates From Sorted Array只有一处变化。这次的变化是第10处的那个判断条件多了一部分 A[len - 1] != A[i] 。从A[0]到A[len]是满足题意要求的部分, 如果现在正在检查的这个element已经在A[0]-A[len]出现过了,但是只要它在最近的两个中没有出现,还是可以加到现在的部分中的。

 class Solution {
public:
int removeDuplicates(int A[], int n) {
// length of satisfied part
int len = -;
for (int i = ; i < n; i++) {
// if it is unique to current satisfied part
// or it has not shown within last two positions
// in current satisfied part
if (A[i] != A[len] || A[len - ] != A[i]) {
len++;
A[len] = A[i];
}
}
return len + ;
}
};

[Leetcode] Remove Duplicates From Sorted Array II (C++)的更多相关文章

  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 从已排序的数组中删除重复元素

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

  3. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  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. nsfocus-笔试题

    1.描述sendmail原理及通讯机制 sendmail程序接受到待发邮件后,通过关键字@判断邮件的格式是否符合要求,匹配成功后提取邮件后缀域名信息并查询DNS数据库相关MX(邮件专用)记录,若有匹配 ...

  2. sp_xml_preparedocument _使用 处理XML文档

      有时会在存储过程中处理一些XML格式的数据,所以会用到sp_xml_preparedocument,他可以将XML数据进行读取,然后使用 MSXML 分析器 (Msxmlsql.dll) 对其进行 ...

  3. Windows进程间通信的各种方法

    原文:Windows进程间通信的各种方法 进程是装入内存并准备执行的程序,每个进程都有私有的虚拟地址空间,由代码.数据以及它可利用的系统资源(如文件.管道等)组成.多进程/多线程是Windows操作系 ...

  4. JAVA常用类库简介(转)

    Java编程语言中为方便学习者学习,编制了许多类,这些类已经经过测试,都是我们编程的基础.如果不利用这些已存在的类,我们的编程工作将变得异常复杂并且效率低下.所以我们应尽可能多的掌握Java基本类库的 ...

  5. 【转】Java如何克隆集合——深度拷贝ArrayList和HashSet

    原文网址:http://blog.csdn.net/cool_sti/article/details/21658521 原英文链接:http://javarevisited.blogspot.hk/2 ...

  6. linux/unix 段错误捕获【续】

    本文为“在C/C++中捕获段错误,打印出错的具体位置”的续篇,进一步解决涉及动态链接库的情况.   背景知识: ·linux/unix下动态链接库的基本原理 ·/proc/pid/maps文件的基本格 ...

  7. 华为u8800怎样root?

    用SuperOneClick就可以root了 .在手机上面进入设置-应用程序-开发-三个都要勾选.用数据线连接到电脑,确认可正常连接.不行就使用豌豆夹连接,豌豆夹会自动帮你安装手机的驱动.运行Supe ...

  8. 教程:30分钟学会Adobe Premiere

    原文地址:http://tieba.baidu.com/p/2785313831 视频教程地址

  9. day55

    担心了好久的编译原理也总是考完了 大学里的最后一次考试也是结束罗 这次的考试起伏跌宕啊 我们本来是9点钟开始考试 但是我们班的几个同学基本上7点钟就去了 为了什么?? 选个好的位置撒哈哈,到了九点,老 ...

  10. 第31讲 UI组件之 Gallery画廊控件

    第31讲 UI组件之 Gallery画廊控件 1.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery只 ...