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. maven-生命周期与插件

    Maven的生命周期是抽象的,具体的操作由插件实现,类似于java的模板设计模式. 1.生命周期 认识生命周期 maven有clean.default.site三种生命周期,每种生命周期都包含一些阶段 ...

  2. [转]Centos7 内核从3.10升级到4.12过程

    [原文地址] http://blog.csdn.net/youshijifen/article/details/73472434 [摘要] 近期,国家互联网应急中心漏洞(CNCERT)公告中提到Lin ...

  3. 微信小程序 提示框延时跳转

    wx.showToast({ title: '成功', icon: 'success', duration: 2000, success:function(){ console.log('haha') ...

  4. 服务器空间不足导致mysql服务器无法运行

    今天有朋友请我帮忙解决一个问题,他公司服务器mysql数据库一直连接失败.登录服务期之后发现服务器空间占满了,导致mysql不能启动. 下面说解决方法: 首先查看空间占用,发现空间占满了 df -h ...

  5. python2.7入门---字符串

        这次咱们就来看一下python的字符串类型.首先我们要知道,字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串.创建字符串很简单,只要为变量分配一个值 ...

  6. SQL Server附加数据库拒绝访问错误解决方法

    今天在MsSQL里附加数据库时提示操作系统错误5(拒绝访问),这里我没给出了两个解决方案供大家解决问题. 方案一:切换登录方式 出现这种情况是由于用“混合验证方式”(SQL Server身份验证)登录 ...

  7. CSS3不一样的下拉选择框

    本例中包含两个下拉选择框的动画示例,本例中并未使用select标签.本例中第一个案例也可用于标题.导航栏等位置. 案例一: html布局 <div class="content&quo ...

  8. LeetCode:3.Longest Substring Without Repeating Characters

    思路:看到题目首先想到最大字符串匹配KMP算法 public static int lengthOfLongestSubstring(String s) { int maxLength = 0; St ...

  9. Django笔记 —— 模板高级进阶

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  10. 『Python Kivy』官方乒乓球游戏示例解析

    本篇文章用于对Kivy框架官方所给出的一个「乒乓球」小游戏的源码进行简单地解析.我会尽可能的将方方面面的内容都说清楚.在文章的最下方为官方所给出的这个小游戏的教程以及游戏源码. 由于篇幅所限,本文只简 ...