题目:

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. Optimize str2date function

    The job can be any string date format convert to AX date format. so that, Do not need to specify str ...

  2. SQLite清空表并将自增列归零[转]

    SQL标准中有 WHERE name = 'TableName'; 也可以直接把该记录删掉: 1 DELETE FROM sqlite_sequence WHERE name = 'TableName ...

  3. c++数组-矩阵的转置

    #include <iostream> using namespace std; int main(){ ][]={{,,},{,,}}; ][]; ;j<;j++){ ;i< ...

  4. 0x02全局变量和局部变量

    全局变量在什么地方定义? .data和.data? 格式如下: 变量名 类型 初始值1,初始值2... 变量名 类型 重复数 dup(初始值1,初始值2,...) 变量名 类型 ? 类型有哪些? 字节 ...

  5. ECSHOP模糊分词搜索和商品列表关键字飘红功能

    ECSHOP联想下拉框 1.修改page_header.lbi模版文件,将搜索文本框修改为: <input name="keywords" type="text&q ...

  6. 【原创】jQuery插件 - Booklet翻书特效教程(一) 一般设置

    jQuery插件 - Booklet翻书特效教程(一) 一般设置 本文由五月雨恋提供,转载请注明出处. 一.宽高(width/height) 1.自定义大小 $(function(){ // 自定义页 ...

  7. [转载+原创]Emgu CV on C# (四) —— Emgu CV on 全局固定阈值二值化

    重点介绍了全局二值化原理及数学实现,并利用emgucv方法编程实现. 一.理论概述(转载,如果懂图像处理,可以略过,仅用作科普,或者写文章凑字数)  1.概述 图像二值化是图像处理中的一项基本技术,也 ...

  8. c# 取 list前100条数据

    [问] List<KeyWord> sortedList = (from a in keyWordList orderby a.Total descending select a).ToL ...

  9. BZOJ 2763: [JLOI2011]飞行路线 spfa dp

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=2763 题解: d[x][kk]表示从s到x用了kk次免费机会的最少花费. 代码: #in ...

  10. ContextLoaderListener作用详解(转)

    ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在 ...