描述

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. 【原】Java学习笔记029 - 映射

    package cn.temptation; import java.util.HashMap; import java.util.Map; public class Sample01 { publi ...

  2. poi 导入导出的api说明(大全)

    原文链接:http://www.cnblogs.com/qingruihappy/p/8443101.html poi 导入导出的api说明(大全) 一. POI简介 ApachePOI是Apache ...

  3. 同一张表省市县sql查询

    一,表的结构 SELECT * FROM t_unionpay_areacode t SELECT * FROM t_unionpay_areacode t WHERE t.`name`LIKE &q ...

  4. PyTorch官方中文文档:torch.Tensor

    torch.Tensor torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU te ...

  5. MySQL根据出生日期计算年龄的五种方法比较

    方法一 SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 AS age 方法一,作者也说出了缺陷,就是当日 ...

  6. 用Tortoisegit往GitHub上push时,失败并显示git did not exit cleanly (exit code 1),可能是GitHub的Email的原因

    之前我看到错误,总是没有耐心地读完整个错误,而是不假思索地搜索一部分错误,导致偏离正确轨道,相当于号错脉了,比如这里只是搜索git did not exit cleanly (exit code 1) ...

  7. C# 登录 存Session ,cookie并且验证只能一个人登录以及清session并且Cache

    string id = ConvertHelper.GetString(ds.Tables[0].Rows[0]["Uid"]);//用户ID string struserid = ...

  8. wpf研究之道-grid控件

    想要说些什么,却不知道从哪开始."形而上谓之道,形而下谓之器".与其坐而论道,不如脚踏实地,从最实用的地方开始. 我们先来看看wpf中的grid控件.grid控件是个网格的布局控件 ...

  9. Oracle-数据类型为date 日期查询技巧

    date类型是oracle中存储日期类的一种常用类型,其处理也是在数据库使用中比较多需要注意的地方.如我们可以使用to_char函数将其转化为任意时间格式的字符串,也可使用to_date函数转化相应的 ...

  10. 筛法求素数Java

    输出:一个集合S,表示1~n以内所有的素数 import java.util.Scanner; public class 筛法求素数 { public static void main(String[ ...