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. 克隆git仓库中的一个分支

    克隆git仓库中的某一个分支,可用如下命令: git clone -b <branch_name> <repo> 如:git clone -b hdcp_ree_tee_dev ...

  2. Scala 中Null, None, Nothing, Nil

    转自:http://blog.csdn.net/bluejoe2000/article/details/30465175 在scala中这四个类型名称很类似,作用确实完全不同的. None是一个obj ...

  3. AndroidStudio0.5.2 BUG 导致 menu 菜单键崩溃

    郁闷了半天,今天发现一点击手机 menu 键应用就崩溃了,记得之前都是好好的,调试了半天代码还是搞不定,于是网上google了一番,发现仅国外有一两篇文章有提到类似问题,据说是 0.5.2 版本的 B ...

  4. magento里的session传值

    1.$registrationCode = Mage::getSingleton('customer/session' )->setData('login_phone_code', $valid ...

  5. niceScroll接口大全

    Query滚动条插件兼容ie6+.手机.ipad http://www.areaaperta.com/nicescroll/ jQuery(function($){ $("#scrollIn ...

  6. 默认conf指向位置

    查看nginx 默认配置文件所在位置 >> nginx -t  print --> nginx: the configuration file /etc/nginx/nginx.co ...

  7. Qt出现常量有换行符的错误的解决方法

    可以使用 QString::fromLocal8Bit 来将本地字符编码转换为 Unicode 形式的 QString.

  8. [编码解码] Base64 编码换行和+号遍空格的处理

    Android自身带有Base64加密与解密的包,可以方便地加密密码方便传输. String base64Token = Base64.encodeToString(token.trim().getB ...

  9. url地址数据转换成json数据格式

    var urlToJson = function(){ var ret = {}; window.location.search.substr(1).replace(/(\w+)=(\w+)/ig, ...

  10. 各个Maven仓库镜像(包括国内)

    各个Maven仓库镜像(包括国内) 衽孤魍墓 ゅ槭 众矿工唯唯诺诺我在旁哭笑不得原 宦蠃サ 骘猩池 粑涫汾滹 吧滔哌蹋 飑俗た 狃攵庾唾 想必是想挡住什么我想反正这笔筒也不是 翡蜮胼 娴左 ...