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. 20165305 《网络对抗技术》 Kali安装

    一.安装kali 在虚拟机中安装kali我参考了下面的网页,里面写的很全面,所以我就不重复了,我主要说一下kali里面的环境配置. 在虚拟机中安装kali linux 注意:输入用户和密码时,kali ...

  2. vue.js国际化vue-i18n插件的使用问题,在模版文本、组件方法、jsf方法里的使用

    vue.js国际化vue-i18n插件的使用问题,在模版文本.组件方法.jsf方法里的使用 1.在文本里使用{{$t("xxx")}} <span>{{$t(" ...

  3. 了解Linux操作系统发展阶段

    一.硬件与软件发展历史 计算机由硬件和软件组成结构 二.Linux的发展史 Linux 操作系统是Unix操作系统的一种克隆系统.它诞生于1991年的10月5日(只是第一次正式向外公布的时间).以后借 ...

  4. IDEA Failed to load dx.jar

    IDEA-177053 Android app crashes on build "Failed to load dx.jar" Error:Android Pre Dex: [c ...

  5. BIOS备忘录之SPI(fingerprint)设备

    Reset和INT信号使用的是GPIO功能,需要显式的使用(INT信号使用了GPIO的int number,RST信号使用了GPIO的absolute number): 问题举例 漏电导致功能异常:在 ...

  6. 外星人入侵游戏(python代码)

    https://github.com/ehmatthes/pcc/tree/master/chapter_12/images

  7. Yii2 mysql查询 int自动变string解决办法

    原因是PDO以string查询数据导致. 这个与YII没关系,是PDO的默认处理,解决方法只需在配置中的db配置中加上attributes的相关配置就行了,如下: 'components' => ...

  8. postgres跨平台开发坑之空值

    ngx_lua架构下查询linux版postgres时,如果目标字段的值返回空,则返回结果为 ngx.null,同样的代码如果查询windows版postgres时,如果目标字段的值返回空,则返回结果 ...

  9. 剑指offer 14:链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点. /* public class ListNode { int val; ListNode next = null; ListNode(int va ...

  10. 11_vim

    vim编辑器 文本编辑器,字处理器linux重要哲学思想之一:使用纯文本格式来保存软件的配置信息,大多数情况下都是如此,而且一切皆文件此前学过nano,sed..nano入门简单,但功能简陋 vi:V ...