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

题意:可以保留一个重复的元素。

思路:第一种是和Remove duplicates from sorted array类似的思想。隔一个比较一次,拿A =[1,1,1,2,2,3]举例子,其对比的过程如下

这里可以将第6行、第10行的2改为3,则至多重复的数字出现三次,代码为:

 class Solution
{
public:
int removeDuplicates(int A[], int n)
{
if(n<=) return n;
int lo=;
for(int i=;i<n;++i)
{
if(A[i] !=A[lo-])
A[lo++]=A[i];
}
return lo;
}
};

方法二:使用计数器,当两者不相等计数器重置,将A[i]赋值给A[lo]的下一位;相等时但是count不超过2,将A[i]赋值给A[lo]的下一位,其余,仅计数器++,这样就可以成功将不等的情况后的,出现第二个和A[i]相等的情况时,将其也赋值给前面的元素。

 class Solution {
public:
int removeDuplicates(int A[], int n)
{
int lo=;
int count=;
if(n<) return n; for(int i=;i<n;i++)
{
if(A[lo]==A[i])
{
count++;
if(count<)
A[++lo]=A[i];
}
else
{
A[++lo]=A[i];
count=;
}
}
return lo+;
}
};

[Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素的更多相关文章

  1. [Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  2. leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

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

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

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

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

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

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

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

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

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

  7. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

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

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

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

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

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

随机推荐

  1. Python栈的学习资料

    持续更新... 1. 基础 Python for Everybody的视频课程,称得上深入浅出 https://www.py4e.com/ 2. 进阶 偏重实践应用,快速上手,稀饭~ https:// ...

  2. Java应用:经纬度匹配(geohash加密)

    本文采用http://gc.ditu.aliyun.com地址进行经纬度匹配,无数量限制 如果给定经纬度进行geohash加密操作,先解密得到相应gps坐标,具体程序如下所示: import java ...

  3. 机器学习基础之knn的简单例子

    knn算法是人工智能的基本算法,类似于语言中的"hello world!",python中的机器学习核心模块:Scikit-Learn Scikit-learn(sklearn)模 ...

  4. ctf题目writeup(8)

    2019.2.11 南京邮电的ctf平台: 地址http://ctf.nuptzj.cn/challenges# 他们好像搭新的平台了...我注册弄了好半天... 1. 签到题,打开网址: 查看一下页 ...

  5. Python自动化运维——IP地址处理模块

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:IPy 功能:辅助我们高效的完成IP的规划工作 安装: wget https://pypi.python.o ...

  6. ABAP CDS ON HANA-(4)ヘッダー行編集

    Explicit Name List use in CDS We create a simple  CDS View like below. @AbapCatalog.sqlViewName: ‘ZS ...

  7. javascript 之 为函数设置默认参数值

    方法一: function example(a,b){ var a = arguments[0] ? arguments[0] : 1;//设置参数a默认为1 var b = arguments[1] ...

  8. @Transactional spring 事务(转载)

    原文链接: http://www.cnblogs.com/sweetchildomine/p/6978037.html?utm_source=itdadao&utm_medium=referr ...

  9. Xshell启动时显示丢失MSVCP110.dll解决方法

    成功安装xshell之后,在运行时却弹出“无法启动此程序,因为计算机中丢失MSVCP110.dll.尝试重新安装该程序以解决此问题”,很多人按照提示重装了还是出现同样的问题,本集教程将具体讲解如何处理 ...

  10. OSI七层模型加协议

    OSI七层网络模型 TCP/IP四层概念模型 对应网络协议 应用层(Application) 应用层 HTTP.TFTP, FTP, NFS, WAIS.SMTP 表示层(Presentation) ...