Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Given 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 respectively.

It doesn't matter what you leave beyond the returned length.

题意:

有序数组去重(允许某个元素重复出现两次)

思路:

仍然是在尽量不开新空间的情况下,直接在原数组中进行操作。(当然,我们可以想象另开了一个新的数组存储去重后的结果)

指针i来遍历原数组。

指针j 从 index 2 开始, 因为index 为 0、1 的元素是什么牛鬼蛇神都不重要。

指针j 接受指针i扫过的、符合条件的元素。

指针j所到之处,便是新“数组”新生之时。

代码:

 class Solution {
public int removeDuplicates(int[] nums) {
// corner case
if(nums.length == 0 || nums == null) return 0;
int j = 2;
for(int i = 2; i < nums.length; i++){
if(nums[i] != nums[j-2]){
nums[j] = nums[i];
j++;
}
}
return j;
}
}

[leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [LeetCode]80. Remove Duplicates from Sorted Array II删除数组中的重复值

    和第一题不同的地方是,容忍两次重复 虽然题目上说只需要长度,但是否检测的时候如果数组不跟着改变也是不行的 没说清楚题意 自己是用双指针做的,看了大神的答案更简单 public int removeDu ...

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

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

  5. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

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

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

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

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

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

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

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  9. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

随机推荐

  1. java android 捕获未处理异常

    1. 定义一个异常处理类 public class ExceptionHandler implements Thread.UncaughtExceptionHandler { public Excep ...

  2. MySQL 命令(导出数据):mysqldump

    官方网址:https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html

  3. Day 11 函数对象,函数嵌套,作用域,闭包

    函数总结 # 函数的定义:def func(a, b):    print(a, b)    return a + b​​# 函数四个组成部分# 函数名:调用函数的依据,必须的# 函数体:执行函数逻辑 ...

  4. Linux之cd、pwd、mkdir、rmdir

    cd.pwd.mkdir.rmdir 命令功能: 切换到指定的目录,可用绝对路径和相对路径 命令格式: cd directory 命令参数: 无 命令实例: 1.切换到/bin目录 vbird@Ubu ...

  5. django操作数据库 ORM

    一,数据库连接 创建类 a. 在models.py中先写类 from django.db import models # 表名是app01_userinfo class UserInfo(models ...

  6. 一次Windows 安装问题

    在 thinpad x250上安装 windows 10时,提示"安装程序无法创建新的系统分区,也无法定位现有系统分区". x250 的主板上自带一个16G的闪存且标识为 主分区, ...

  7. 二叉树遍历(flist)(已知中序和按层遍历,求先序 )

    问题 F: 二叉树遍历(flist) 时间限制: 1 Sec  内存限制: 128 MB提交: 11  解决: 9[提交][状态][讨论版][命题人:quanxing][Edit] [TestData ...

  8. I18nUtils

    import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.*; ...

  9. kubernetes核心组件kube-proxy 学习总结

    一.  kube-proxy 和 service  kube-proxy是Kubernetes的核心组件,部署在每个Node节点上,它是实现Kubernetes Service的通信与负载均衡机制的重 ...

  10. Nios II Host-Based File System

    Nios II Host-Based File System 允许运行在Nios II的程序,在Debug模式下,通过Altera download cable来读写PC上当前工程目录下(及其子目录) ...