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. MVC-工作原理

    ASP.NET MVC的原理,其实就是使用HttpModule和HttpHandler将用户的请求拦截,按照设定的路由规则解释到相应的控制器和Action,加以执行.Module是一个比较宏观一点的概 ...

  2. 【安装】python3.4版安装与2.x共存问题

    首先,到官网去下载python3.x版,这里推荐3.4以上的版本,自带pip库,以后不用自己另外下载 3.4.4版: https://www.python.org/downloads/release/ ...

  3. IIS添加服务

    最近心血来潮,想学习一下WCF,看着网上的一个小例子就开始动手了. 写了一个简单的服务,准备发布时,才发现很多问题,根本不能像网上的那些大神一样“易得”. 其中遇到的一个的典型问题,就是提示为下载的文 ...

  4. 阿里云Linux服务器挂载硬盘分区

    查看所有硬盘与分区 fdisk -l 运行命令 fdisk /dev/xvdb 根据提示,依次输入 n p 1    (数字一 不是 L) 回车 回车 w 提示 Syncing disks.时,表示已 ...

  5. java实现线性表

    /** * 线性表 * @author zyyt * */ public  class LinkList {//框架级别的大师级 private int size;//链表的实际大小 private ...

  6. Registry Workshop(注册表编辑器) V4.6.3 官方中文版

    软件名称: Registry Workshop(注册表编辑器)软件语言: 简体中文授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 1.1MB图 ...

  7. centos redis 安装

    # wget http://download.redis.io/releases/redis-2.8.6.tar.gz # tar xzf redis-2.8.6.tar.gz # cd redis- ...

  8. 升级到macSierra 10.12之后 在模拟器上面滑动视图很卡,

    解决方法 在终端里面输入sudo sysctl -w kern.timer.coalescing_enabled=0

  9. 2016 ACM Amman Collegiate Programming Contest D Rectangles

    Rectangles time limit per test 5 seconds memory limit per test 256 megabytes input standard input ou ...

  10. 小谈数据库Char、VarChar、NVarChar差异

    1. char      固定长度,最长n个字符.   2. varchar      最大长度为n的可变字符串. (n为某一整数,不同数据库,最大长度n不同)   char和varchar区别:   ...