https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby

描述

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].

解析

有序数组,如果要删除值,必须当前位置的值 > 当前位置-2的值.

代码

public int removeDuplicates(int[] nums) {
int i = 0;
for (int n : nums)
if (i < 2 || n > nums[i-2])
nums[i++] = n;
return i;
}

类似问题:[LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)的更多相关文章

  1. [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  2. 080 Remove Duplicates from Sorted Array II 从排序阵列中删除重复 II

    “删除重复项目” 的进阶:如果重复最多被允许两次,又该怎么办呢?例如:给定排序数列 nums = [1,1,1,2,2,3]你的函数应该返回长度为 5,nums 的前五个元素是 1, 1, 2, 2 ...

  3. LeetCode 83. Remove Duplicates from Sorted List (从有序链表中去除重复项)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  4. LeetCode 83. Remove Duplicates from Sorted List(从有序链表中删除重复节点)

    题意:从有序链表中删除重复节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode ...

  5. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

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

  6. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

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

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

  8. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

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

  9. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

随机推荐

  1. Apache Maven 3.6.1配置安装

    Apache Maven 3.6.1配置安装 一.下载 maven下载地址:http://maven.apache.org/download.cgi 二.安装 1,解压即可用 2,环境变量配置 MAV ...

  2. VueJS第2天 初阅API(初识MarkDown)

    指令是带有前缀 v-,以表示它们是 Vue 提供的特殊特性.可能你已经猜到了,它们会在渲染的 DOM 上应用特殊的响应式行为 v-bind --> 数据绑定 v-for --> 循环 v- ...

  3. C# 启动外部进程

    C# 启动外部进程: var processInfo = new ProcessStartInfo(fullName); processInfo.UseShellExecute = false; pr ...

  4. Spring Boot 监控利器 —— Actutor

    参考 CSDN-学习Spring Boot:(二十七)Spring Boot 2.0 中使用 Actuator 使用Actuator监控Spring Boot应用 程序猿DD-Spring Boot ...

  5. ProtoType原型和__Proto__原型链的详解

    var arr = new Array(12,13,14,15,16,17,18); var arr2 = new Array(12,13); arr.sun=function(){ //用原型加Ar ...

  6. Ubuntu: 安装自带的中文输入法

    1. 在终端安装输入法软件包 在终端输入 sudo apt-get install ibus-pinyin 2. 启用输入法 打开设置 找到Region&Language 找到Input So ...

  7. yii 邮箱封装

    <?php class Mailer { private static $obj; private static $config; public static function getMaile ...

  8. python生成可执行exe文件

    为什么要生成可执行文件 不需要安装对应的编程环境 可以将你的应用闭源 用户可以方便.快捷的直接使用 打包工具 pyinstaller 安装pyinstaller pip install pyinsta ...

  9. spring cloud之Feign的使用

    原始的调用客户端的方式是通过注入restTemplate的方式 restTemplate.getForObject("http://CLIENT/hello", String.cl ...

  10. Struts 2 框架搭建HelloWorld

    1.导包 导入相应的jar包,在blank项目中会出现 2.书写Action类 package com.littlepage.struts; public class HelloAction { pu ...