题目:

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

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1122 and 3. It doesn't matter what you leave beyond the new length.

Hide Tags

Array Two Pointers

 

链接:  http://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/

题解:

在排好序的数组里最多保留两个重复数字。设置一个limit,使用一个count,一个result用来计算最终结果。依照count和limit的关系决定是否移动到下一个index。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int limit = 2, count = 1, result = 0; for(int i = 0; i < nums.length; i ++){
if(i > 0 && nums[i] == nums[i - 1])
count ++;
else
count = 1;
if(count <= limit)
nums[result ++] = nums[i];
}
return result;
}
}

Update:

public class Solution {
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int count = 1, index = 1; for(int i = 1; i < nums.length; i++) {
if(nums[i] == nums[i - 1])
count ++;
else
count = 1;
if(count <= 2)
nums[index++] = nums[i];
} return index;
}
}

二刷:

就是设置一个limit,设置当前count为1,用来返回结果的index为1.

每次在循环里尝试更新count, 假如nums[i] = nums[i - 1]则count++,否则count = 1

在count <= limit的条件下,我们可以更新num[index++] = nums[i]。

最后返回index

Java:

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public int removeDuplicates(int[] nums) {
int limit = 2, count = 1, index = 1;
for (int i = 1; i < nums.length; i++) {
count = nums[i] == nums[i - 1] ? count + 1 : 1;
if (count <= limit) {
nums[index++] = nums[i];
}
}
return index;
}
}

三刷:

几天没刷题,大脑反应速度就不够了,想得也细致,不能透过现象看本质。

Java:

public class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int k = 2, count = 1, lo = 1;
for (int i = 1; i < nums.length; i++) {
if ((nums[i] == nums[i - 1] && count < k) || (nums[i] != nums[i - 1])) {
count = (nums[i] == nums[i - 1]) ? count + 1 : 1 ;
nums[lo++] = nums[i];
}
}
return lo;
}
}

Update:

使用二刷的逻辑

public class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int limit = 2, count = 1, lo = 1;
for (int i = 1; i < nums.length; i++) {
count = (nums[i] == nums[i - 1]) ? count + 1 : 1 ;
if (count <= limit) nums[lo++] = nums[i];
}
return lo;
}
}

Update:

使用Stefan的代码

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

Reference:

https://leetcode.com/discuss/42348/3-6-easy-lines-c-java-python-ruby

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. C# 枚举,传入int值返回string值

    需求:1:子公司负责人2:人事3:审批人4:签批人 5:管理员  传入值为1,2,3,4,5这个数字的某一个.需要返回他们的中文描述. 一下忘记该怎么写了...后来百度下查出来了..记录下当个小工具吧 ...

  2. Linux磁盘与文件系统管理

    df df(disk free) 功能说明:显示磁盘的相关信息.语 法:df [-ahHiklmPT][--block-size=<区块大小>][-t <文件系统类型>][-x ...

  3. 理解PHP 依赖注入|Laravel IoC容器

    看Laravel的IoC容器文档只是介绍实例,但是没有说原理,之前用MVC框架都没有在意这个概念,无意中在phalcon的文档中看到这个详细的介绍,感觉豁然开朗,复制粘贴过来,主要是好久没有写东西了, ...

  4. VBS基础篇 - 运算符

    VBScript 有一套完整的运算符,包括算术运算符.比较运算符.连接运算符和逻辑运算符. 运算符优先级: 首先计算算术运算符,然后计算比较运算符,最后计算逻辑运算符. 所有比较运算符的优先级相同,即 ...

  5. Extmail maildrop错误

    错误: <liuyb@5teacher.com>: Command died with status 127: "maildrop". Command output: ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Android SDK Android NDK 官方下载地址

    Android NDK r6b Windows http://dl.google.com/android/ndk/android-ndk-r6b-windows.zip Mac OS X(intel) ...

  8. Http之Get/Post请求区别

    Http之Get/Post请求区别 1.HTTP请求格式: <request line> <headers> <blank line> [<request-b ...

  9. dbutils 执行sql返回的数据类型

    //ArrayHandler: 把结果集中的第一行数据转成对象数组 //ArrayListHandler:把结果集中的每一行数据都转成一个对象数组,再存放到List中 //BeanHandler:将结 ...

  10. 3223: Tyvj 1729 文艺平衡树 - BZOJ

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...