算法题丨Remove Duplicates from Sorted Array II

时间:2022-09-26 21:50:30

描述

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&lpar;移除有序数组中重复的两次以上的数字&rpar;

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

  3. &lbrack;LeetCode&rsqb; Remove Duplicates from Sorted Array II &lbrack;27&rsqb;

    题目 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&period; Remove Duplicates from Sorted Array &amp&semi;&amp&semi; Remove Duplicates from Sorted Array II &amp&semi;&amp&semi; Remove Element

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

  6. Remove Element&comma;Remove Duplicates from Sorted Array&comma;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 &lbrack;Array&sol;auto&rsqb; &lt&semi;c&plus;&plus;&gt&semi;

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  8. 【LeetCode】80&period; Remove Duplicates from Sorted Array II &lpar;2 solutions&rpar;

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

  9. &lbrack;leetcode&rsqb; 80&period; Remove Duplicates from Sorted Array II &lpar;Medium&rpar;

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

随机推荐

  1. Hadoop openssl false

    错误如图 检查Hadoop native 经过: 1. 重新编译cdh的hadoop2.5.0,复制native 2. 重新格式化namenode 都不行,另外openssl和openssl-dev都 ...

  2. 每次点击按钮后,判断页面是否已经有该行,没有弹出repeater的一行,并给他赋一个这行附值,没有则跳出

    protected void btnAdd_click(object sender, EventArgs e) { try { //记录第几次追加 pressCount++; typeString.A ...

  3. 洛谷P1209 &lbrack;USACO1&period;3&rsqb;修理牛棚 Barn Repair

    题目描述 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. 牛棚一个紧挨着另一个被排成一行,牛就住在里面过夜. 有些牛棚里有牛 ...

  4. hibernate环境配置和使用

    一.hibernate简单介绍                Hibernate是一个开放源码的对象关系映射框架,它对JDBC进行了很轻量级的对象封装,使得Java程序猿能够随心所欲的使用对象编程思维 ...

  5. 必须掌握的八个cmd命令

    原文:必须掌握的八个cmd命令 一.ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的 ...

  6. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

  7. &lbrack;sklearn&rsqb; 官方例程-Imputing missing values before building an estimator 随机填充缺失值

    官方链接:http://scikit-learn.org/dev/auto_examples/plot_missing_values.html#sphx-glr-auto-examples-plot- ...

  8. python 系列文章汇总&lpar;持续更新&period;&period;&period;)

    引言 不知不觉已经写了好几篇 python 相关的随笔了,从刚开始的门外汉到现在已经对 python 有一些入门了,时间也已经过去了一个多月. 写博客真是好处多多,不仅能提供整理自己学习的知识点,梳理 ...

  9. vagrant特性——基于docker开发环境(docker和vagrant的结合)-3-boxes和配置

    Docker Boxes Docker provider不需要vagrant box.因此其config.vm.box设置是完全可选的.但是,仍然可以使用并指定一个box来提供默认值.由于一个带着bo ...

  10. 【mongdb主从复制和同步】

    主从同步: Master: Slave: 副本集: #在卷本中加任意主机 #登录从 #登录主 #同步日志 #仲裁: 向集群中添加主机成为仲裁 #查看集群里的成员角色参数: