题目:

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. mysql基本知识---20151127-1

    2015年11月27日,作为PHPer的我开始全面学习mysql数据库. 基本语法: 1.连接服务器: mysql>mysql -h host -u root -p 回车 输入密码(本地环境可以 ...

  2. Ztack学习笔记(1)-初识Ztack

    一.Zigbee协议 Zigbee是IEEE 802.15.4协议的代名词,是一种短距离.低功耗的无线通信技术.这一名称来源于蜜蜂的八字舞,因为蜜蜂(bee)是靠飞翔和“嗡嗡”(zig)地抖动翅膀的“ ...

  3. wpf 仿QQ音乐歌词卡拉OK

    最近用WPF做了个音乐播放器,读取歌词.歌词同步都已经实现了.卡拉OK逐字变色 也实现了,但是逐字变色时不能根据歌手唱的快慢来逐字显示.请问各位大神,这个如何解决,有何思路?(附上我做的界面) 感谢各 ...

  4. MySQL 简洁连接数据库方式

    OS  :   CentOS 6.3 DB  :  5.5.14 MySQL连接数据库的方式很多: 1.[root@db01 bin]# ./mysql -uroot -p 2.[root@db01 ...

  5. OJ网站程序员必备

    一. Online Judge简介: Online Judge系统(简称OJ)是一个在线的判题系统.用户可以在线提交程序多种程序(如C.C++.Pascal)源代码,系统对源代码进行编译和执行,并通过 ...

  6. IIS支持PHP

    1. 解压php-5.2.6.zip到D:\php5,找到php.ini-dist改名为php.ini并将它放到C:\WINDOWS目录下. 2. 将D:\ php5目录下的libmcrypt.dll ...

  7. 【iOS】Objective-C简约而不简单的单例模式

    前些日子在项目中因为误用了单例而导致了一系列问题.原来在objective-c中的单例并没有java或者C#那么简单的实现,这里记录下: 问题是这样被发现的,在对于一个UIViewController ...

  8. perl 脚本测试

      原文地址:  http://blog.csdn.net/johnny710vip/article/details/8905239   这是一篇关于perl脚本测试的总结性文章,其中提到了很多实用的 ...

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

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

  10. 3563: DZY Loves Chinese - BZOJ

    Description神校XJ之学霸兮,Dzy皇考曰JC.摄提贞于孟陬兮,惟庚寅Dzy以降.纷Dzy既有此内美兮,又重之以修能.遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上有N座祭 ...