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.

Example 1:

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.

Example 2:

Given nums = [0,0,1,1,1,1,2,3,3],

Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.

It doesn't matter what values are set beyond the returned length.
class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null) {
return 0;
}
if (nums.length < 2) {
return nums.length;
}
int slow = 2;
for (int i = 2; i < nums.length; i++) {
if (nums[i] != nums[slow - 2]) {
nums[slow++] = nums[i];
}
}
return slow;
}
}

[LC] 80. Remove Duplicates from Sorted Array II的更多相关文章

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

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

  2. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

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

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

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

  5. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  6. [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% ...

  7. **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II

    1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...

  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 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

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

随机推荐

  1. 24.docker 部署 wordPress

    1. 拉取远程 mysql 和 wordpress 镜像 并 启动起来 使用 docker pull mysql:5.7.27 docker pull wordpress 2. 创建mysql 的 c ...

  2. Celery架构

    Celery 官方 # Celery 官网:http://www.celeryproject.org/ # Celery 官方文档英文版:http://docs.celeryproject.org/e ...

  3. 关于Spring的使用总结

    简介 Spring框架是一种轻量级的.一站式的企业级开发解决方案 框架(framework):是一组设计思想.规范.API的精心组合,专门用来解决某一层次或领域的问题 轻量级(lightweight) ...

  4. 远程调用shell脚本文件和远程复制文件

    1.安装sshpass yum install sshpass 2.本地调用远程服务器的shell脚本文件: sshpass -p sa ssh root@192.168.56.105 -C &quo ...

  5. MACOSX下查看某个端口被哪个程序占用及杀进程方法

    sudo lsof -i :9000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 61342 a 313u IPv6 0x11111 ...

  6. set theory

    set theory Apart from classical logic, we assume the usual informal concept of sets. The reader (onl ...

  7. Java之关键字abstract(抽象类与抽象方法)

    /* * abstract关键字的使用 * 1.abstract:抽象的 * 2.abstract可以用来修饰的结构:类.方法 * * 3. abstract修饰类:抽象类 * > 此类不能实例 ...

  8. 洛谷-P3796-【模板】AC自动机(加强版)

    题目传送门 -------------------------------------- 过年在家无聊补一下这周做的几道AC自动机的模板题 sol:AC自动机,在fail边的基础上再加一个last边, ...

  9. centos 7 安装及配置vsftpd

    一.防火墙开放21端口 二.创建FTP用户,创建完用户后在/etc/passwd里是这样呈现的:ftpuser:x:1008:1008::/var/www/html:/sbin/nologin 三.y ...

  10. 在Orcl中通过SQL语句修改创建表

    1.创建表时定义唯一性约束 CREATE TABLE table_name ( column1 datatype null/not null, column2 datatype null/not nu ...