LeetCode OJ Remove Duplicates from Sorted Array II
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 1
, 1
, 2
, 2
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的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [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% ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- [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 ...
- [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 ...
- LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- leetcode 【 Remove Duplicates from Sorted Array II 】python 实现
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [LeetCode#82]Remove Duplicates from Sorted Array II
Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...
随机推荐
- Fedora24 升级到25
1 安装dnf-plugin-system-upgrade dnf install dnf-plugin-system-upgrade 2 更新软件包 dnf system-upgrade downl ...
- [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads
给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...
- css基础和心得(一)
css开始 p{ font-size:12px; color:red; font-weight:blod } p为选择符p{}又称选择器,指明网页中要应用样式规则的元素.属性和值之 ...
- js---疑点代码段解析
function count() { var arr = []; for (var i=1; i<=3; i++) { console.log("iii---"+i); ar ...
- 【Python】functools.wraps定义函数装饰器
第一次见到functools.wraps是在 Flask Web开发 中,一直不明白怎么回事. 装饰器(decorator)是干嘛的?对于受到封装的原函数来说,装饰器能够在那个函数执行前或者执行后分别 ...
- .net MVC 中“MvcPager” 插件的简单使用。
.net MVC 中提供了一个分页组件"MvcPager",用起来还算方便,实用性较强. 简单写一下使用方法,如有不足的地方,请各位大大给小弟指正出来. 一.准备工作 使用这个组件 ...
- MySQL字段联合去重sql
MySQL字段联合去重sql 例如数据 id,name1,name2 1 a x 2 a y 3 b x 4 a y 5 a x 联合去重name1,name2的结果为 id,name1,name2 ...
- SVN解锁失败的解决办法
背景:在版本发布后,本人把工程文件上锁后,进行过修改但没有进行上传.后面一段时间后,开发人员需要进行修改提交,发现解锁不成功. 使用解锁命令时:提示如下: If you want to break t ...
- Spring 配置文件XML -- <beans>中属性概述
beans : xml文件的根节点. xmlns : XML NameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上 ...
- js中三目运算符和&& || 符的个人浅见
这两天看到别人写的代码,感觉很牛逼,如下,大神请忽视 $(".lgn").on("click", function() { var a = {}; a.logi ...