描述

Follow up for "Remove Duplicates":

What if duplicates are allowed at most twice?

示例

Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums
being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.

算法分析

难度:中

分析:条件参考Remove Duplicates算法,只不过之前元素只允许重复1次,现在改为最多可以重复2次。

思路:既然输入的数组排好序的,那我们定义一个有效元素的数组长度变量i,然后开始遍历元素:

 1. 检索当前元素之前的2个元素,如果跟当前元素相等,说明不满足最多重复2次的条件了,这时候有效元素的数组长度i就不要再自增了;

 2. 否则的话,之前的2个元素,如跟当前元素不相等(小于),说明这个当前元素为有效值,所以将数组末尾元素赋值当前元素,有效元素的数组长度i自增1;

依照上述逻辑循环判断,一直到数组最后一个元素,循环结束后,根据有效元素的数组长度i,获得[0,i)范围内的数组元素,即为题目要求的结果。

代码示例(C#)

public int RemoveDuplicates2(int[] nums)
{
int i = 0;
foreach (var num in nums)
{
//判断是否有2个以上重复,有的话有效索引+1,并将当前元素赋值到有效数组
if (i < 2 || num > nums[i - 2])
nums[i++] = num;
}
return i;
}

复杂度

  • 时间复杂度O (n).
  • 空间复杂度O (1).

附录

算法题丨Remove Duplicates from Sorted Array II的更多相关文章

  1. 算法题丨Remove Duplicates from Sorted Array

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

  2. 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  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

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  5. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  6. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  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】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

随机推荐

  1. [mysql] 2进制安装和简单优化

    ##################################mysql 2进制安装和简单优化################################################## ...

  2. zjoi网络

    map加LCT水一下就过了 # include <stdio.h> # include <stdlib.h> # include <iostream> # incl ...

  3. css边框小结

    css边框 CSS对界面的分割如上图,他们的含义如下: contend:包含HTML元素中包含的文本,图像或其他媒体.      padding:内容和边框之间的空格. 你可以想像这样的内在空间.   ...

  4. 关于 CI框架访问数据库类提示Call to undefined function mysqli_init() 问题解决

    我上次实践发现,安装在Win10 WampServer3.0.4集成环境,不仅打不开phpmyadmin会报错就算了,而且报错后又没提示那么解决,同时你打开php扩展配置发现,WampServer系统 ...

  5. 在linux上安装dotnetcore

    dotnet core已经出来有一段时间了,不是什么新名词了.但这个技术,目前还是比较新的,企业也没有普遍应用.它最大的亮点就是跨平台,也就是我们写的c#代码,可以运行在linux上. 在国内学习do ...

  6. WebService下实现大数据量的传输

    设置RemotingFormat = SerializationFormat.Binary;再序列化,通过WebService传输,客户端接收,再反序列化,确实效果大大的优于直接传送DataSet,不 ...

  7. log4net应用实践(一)

          1.背景 log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具.它是.Net下一 ...

  8. Redis学习日记-01

    Redis是什么东东? Redis是用C语言开发的Key-Value数据库,说是数据库,其实他是NoSql(非关系型数据库). 这里顺便说一下Sql(关系型数据库,如MySql,Oracle等)和No ...

  9. WebService就是这么简单

    WebService介绍 首先我们来谈一下为什么需要学习webService这样的一个技术吧.... 问题一 如果我们的网站需要提供一个天气预报这样一个需求的话,那我们该怎么做????? 天气预报这么 ...

  10. Activiti就是这么简单

    Activiti介绍 什么是Activiti? Activiti5是由Alfresco软件在2010年5月17日发布的业务流程管理(BPM)框架,它是覆盖了业务流程管理.工作流.服务协作等领域的一个开 ...