描述

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. org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir

    1.错误描述 org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir svn: Co ...

  2. 一款PHP环境整合工具—VertrigoServ介绍

    Vertrigo简介 VertrigoServ 是一个Windows平台下的非常专业的.易于安装的免费网络开发环境,它集成了Apache, PHP, MySQL, SQLite, SQLiteMana ...

  3. Django学习-14-分页功能实例

    首先创建一个制作page的工具类                     utils                         --page_make.py                    ...

  4. pat1041-1050

    没想到半天就做完了10题 = =,这几题太简单了,基本10分钟一题 1041 #include<cmath> #include<map> #include<iostrea ...

  5. linux用户和群组

    1.用户的主要群组和次要群组   切换用户:su -username 查看群组:#vi /etc/passwd         //主要群组                  #vi /etc/gro ...

  6. Web开发工具——Jupyter notebook

    jupyter-notebook 安装及远程访问 Introduction Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程 ...

  7. iOS9 HTTP 网络访问问题

    今天升级Xcode 7.0 发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Security po ...

  8. hbase 导入导出、hbase shell 基本命令。

    数据导入 ./hbase org.apache.hadoop.hbase.mapreduce.Driver import  表名    数据文件位置hdfs数据文件位置 可以加 前缀 file:/// ...

  9. MySQL单表百万数据记录分页性能优化,转载

    背景: 自己的一个网站,由于单表的数据记录高达了一百万条,造成数据访问很慢,Google分析的后台经常报告超时,尤其是页码大的页面更是慢的不行. 测试环境: 先让我们熟悉下基本的sql语句,来查看下我 ...

  10. ios开发数据库版本迁移手动更新迭代和自动更新迭代艺术(二)

    由于大家都热衷于对ios开发数据库版本迁移手动更新迭代和自动更新迭代艺术(一)的浏览下面我分享下我的源文件git仓库: 用法(这边我是对缓存的一些操作不需要可以省去):https://github.c ...