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. python -yield理解

    参考:https://foofish.net/iterators-vs-generators.html 从网上看到一个面试题,求最后的输出结果: def add(n, i): return n+ide ...

  2. rabbitMq及安装、fanout交换机-分发(发布/订阅)

    <dependency>            <groupId>com.rabbitmq</groupId>            <artifactId& ...

  3. 团队的Kick off

    团队名称:Super power 团队介绍:我们是一个积极向上不乏活力快咯的团队,在一起的任务是happy高效地完成我们的项目. 团队成员自我介绍: 李洪超(项目经理):(男,帅)性格内向,爱好学习, ...

  4. WebStorm过期解决方法

    第一步:先删除C:\Users\本机用户名\.WebStorm2016.3\config文件中eval文件. 第二步:打开webstrom 如图,填写许可证激活码:http://idea.imsxm. ...

  5. Redis连接池

    package com.lee.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; impor ...

  6. 论文笔记【三】A Deep Dive into Word Sense Disambiguation with LSTM

    深入理解LSTM词义消歧 Minh Le,Marten Postma,Jacopo Urbani和Piek Vossen 阿姆斯特丹自由大学语言,文学和传播系 阿姆斯特丹自由大学计算机科学系 摘要 基 ...

  7. STM32时钟

    https://blog.csdn.net/qq_29350001/article/details/81409693 这是个大佬讲的 F429有5个时钟源,HSI,HSE,LSI,LSE;PLL; 对 ...

  8. day16

    列表生成式语法[表达式 for in 遍历 if 条件]会从li一次去除所有值,进行判断 如果满足条件 就装到新的列表里 生成数据的函数函数体中又yield关键字yield 暂停函数的执行 还能返回一 ...

  9. CentOS中使用tcpdump抓包

    安装: yum install tcpdump 命令使用: 监听特定网卡 tcpdump 抓取第一块网卡所有数据包 [root@server110 tcpdump]# tcpdump tcpdump: ...

  10. 编码原则 之 Once and Only Once

    原文 The Once and Only Once principle can be thought of as a subset of the Don’t Repeat Yourself princ ...