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.


【题目分析】

在前一个题目的基础上,这个题目稍微做了变化。上一个题目中每一个数字只能出现一次,在这个题目中每一个数字允许出现两次。


【思路】

回忆一下,在Remove Duplicates from Sorted Array这个题目中,一个巧妙的方法就是设置了两个下标 i 和 j,i 用来遍历数组,j 用来指示数组中不重复元素最后一个元素的位置。那么同样在这个题目中我们也设置这样两个变量,指示此时的 j 用来指示数组中重复次数不超过2的元素最后的位置。我们怎么知道一个元素重复出现的次数呢?难道要设置一个变量来记录当前元素重复出现了多少次吗?一个很巧妙的办法就是比较当前元素nums[i] 是否和前前一个元素 nums[j-1] 相同,如果不相同则nums[++j] = nums[i],否则的话当前元素出现次数肯定是大于2次了,继续向后遍历数组即可。

这个过程如下:

  • j = 1; i = 2;

  • num[i] 等于 nums[j -1]; i++;

  • nums[i] 不等于 nums[j -1]; nums[++j] = nums[i]; i++;

  • num[i] 不等于 num[j -1]; nums[++j] = nums[i]; i++;

  • num[i] 等于 num[j -1] ; i++;

  • num[i] 不等于 num[j -1]; nums[++j] = nums[i];


【java代码】

 public class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length <= 2)
return nums.length;
int j = 1;
for(int i=2; i<nums.length; i++) {
if (nums[j-1] != nums[i])
nums[++j] = nums[i];
}
return j+1;
}
}
 

LeetCode OJ 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】Remove Duplicates from Sorted Array II

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

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

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

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

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

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

  7. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

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

  8. leetcode 【 Remove Duplicates from Sorted Array II 】python 实现

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

  9. [LeetCode#82]Remove Duplicates from Sorted Array II

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

随机推荐

  1. Genymotion下载失败解决方法

    Genymotion下载虚拟机版本时会很慢,而且经常下载失败 解决方法如下: 1.先去选择下载你需要的版本,之后会下载(很慢),或者失败. 2.到C:\Users\yourname\AppData\L ...

  2. [MFC美化] SkinSharp使用详解2-SkinH.h函数介绍

    SkinSharp功能强大,该皮肤库支持完全多种颜色改变等. 下面是静态链接库时的SkinH.h头文件: /*在Stdafx.h文件中加入如下语句 #include "SkinH.h&quo ...

  3. python字符串和列表

    import sys#sys.argv[0] 被设定为指定模块的全名#脚本名和附加参数传入一个名为 sys.argv 的字符串列表.你能够获取这个列表通过执行 import sys,列表的长度大于等于 ...

  4. 在ubuntu14.04上安装openstack mitaka

    最近在工作环境安装部署了juno版本,在GE口测试网络性能不太满意,发现mitaka版本支持ovs-dpdk,于是抽时间安装实验一番. 参考官网的安装文档,先准备将mitaka版本安装好再配置ovs. ...

  5. MyBatis 学习-与 Spring 集成篇

    根据官方的说法,在 ibatis3,也就是 Mybatis3 问世之前,Spring3 的开发工作就已经完成了,所以 Spring3 中还是没有对 Mybatis3 的支持.因此由 Mybatis 社 ...

  6. mac 下mysql

    We've installed your MySQL database without a root password. To secure it run: mysql_secure_installa ...

  7. c++中宽字节表示

    1.C++语言中_T Visual C++里边定义字符串的时候,用_T来保证兼容性,VC支持ascii和unicode两种字符类型,用_T可以保证从ascii编码类型转换到unicode编码类型的时候 ...

  8. Python基础知识学习_Day3

    一.字典用法 字典是一种key-value数据类型,通过key获取具体value的内容,字典的特性是无序.去重. 增删改查用法如下: 1.1基本增删改查操作 name = {"," ...

  9. ios GCD的使用及封装

    实现代码: CGDHelper /* * Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. * 系统要求:iOS4.0以上. */ #import & ...

  10. non-ARC代码转 ARC 排除 “Existing instance variable 'delegate' for property with assign attribute must be _unsafe _unretained” 错误

    原来非ARC代码是 @interface MHWebImageDownloader : NSObject { id<MHWebImageDownloaderDelegate> delega ...