题目:

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. SSL handshake failed: SSL 错误:在证书中检测到违规的密钥用法。

    问题:在WINDOWS中创建的SVN Server,在Linux client中无法连接.原因:WINDOWS中的证书无法被Linux正确识别,因此需要修改证书,以使双方都可以正确识别. 修改方法如下 ...

  2. Activity的Launch mode详解 singleTask正解

    Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance.以下逐一举例说明他们的区别: standard:Activity ...

  3. MySQL 主键冲突,无法插入数据

    数据库版本:5.6.16 问题:开发来电话说仓库无法下单,程序插入数据提示:入库单 xxxx1589762285确认失败:Duplicate entry '8388607' for key 'PRIM ...

  4. TQ2440开发板网络配置方式

    一.命令行模式 1.设置IP.子网掩码(netmask) #ifconfig eth0 <IP地址> netmask <子网掩码> up up 表示开启网卡eth0,可以不加 ...

  5. ubuntu 下dbus的环境搭建和使用

    从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && ...

  6. c++各种排序

    1.插入排序 void InsertSort(int a[], int n) { int temp, i, j; ; i < n; i++) { ]) { temp = a[i]; ; j &g ...

  7. fawef

    欢迎使用马克飞象 @(示例笔记本)[马克飞象|帮助|Markdown] 马克飞象是一款专为印象笔记(Evernote)打造的Markdown编辑器,通过精心的设计与技术实现,配合印象笔记强大的存储和同 ...

  8. Linux Shell编程变量赋值和引用

    我们可以使用任意一种文字编辑器,比如gedit.kedit.emacs.vi等来编写shell脚本,它必须以如下行开始(必须放在文件的第一行):   #!/bin/sh  ...  注意:最好使用“! ...

  9. c语言编程之栈(数组实现)

    用数组实现的顺序栈,完成了出栈入栈功能. #include"stdio.h" typedef int element; #define max 100 typedef struct ...

  10. 手写SqlHelper

    SqlHelper是一个基于.NET Framework的数据库操作组件.组件中包含数据库操作方法,SqlHelper有很多版本,主要以微软一开始发布的SqlHelper类,后面包含进了Enterpr ...