[LeetCode#82]Remove Duplicates from Sorted Array II
Problem:
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.
Analysis:
A wrong solution:
<When you update on a array and check on the array, you must be careful about if you get the original data or updated date>
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
if (nums.length <= 2)
return nums.length;
int count = 2;
for (int i = 2; i < nums.length; i++) {
if (nums[i] == nums[i-1] && nums[i-1] == nums[i-2])
continue;
count++;
nums[count-1] = nums[i];
}
return count;
} Problem 1:
This solution is ugly!!! The code
if (nums[i] == nums[i-1] && nums[i-1] == nums[i-2])
continue;
count++;
nums[count-1] = nums[i]; The above code could be written into:
if !(nums[i] == nums[i-1] && nums[i-1] == nums[i-2])
nums[count] = nums[i];
count++; Problem 2:
The solution has implemention logic error.
<When you update on a array and check on the same array, you must be careful about if you get the original data or updated date>
Cases:
1, 1, 1, 2, 2
After interation: i == 3,
1, 1, (2), 2, *2
At interation: i == 4
We could see
nums[4] == nums[3] && nums[3] == nums[2]
Which is wrong!!! we replaced nums[2] with 2, but nums[3] still in it's original position. We lose the information of original nums[2]. How could we solve this problem???
A great idea: check if (nums[i] != nums[count-2])
Note: the count pointer always point to the next avaiable position.
nums[count-1] means the last element we place into nums.
nums[count-2] means the last two element we place into nums. Keep on thing in mind, if the current element num[i] has already been appeared more than two times, it must be nums[count-1] and nums[count-2]. !!! And if nums[count-2] == nums[count], it means nums[count-2] must equal to nums[count-1].
If not, we could not skip it!
if (nums[i] != nums[count-2]) {
nums[count] = nums[i];
count++;
} Genius thinking!
Solution:
public class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
if (nums.length <= 2)
return nums.length;
int count = 2;
for (int i = 2; i < nums.length; i++) {
if (nums[i] != nums[count-2]) {
nums[count] = nums[i];
count++;
}
}
return count;
}
}
[LeetCode#82]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 OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- codevs 1519 过路费 最小生成树+倍增
/*codevs 1519 过路费 最小生成树+倍增*/ #include<iostream> #include<cstdio> #include<cstring> ...
- web 页面传值方法
一. 使用QueryString变量 QueryString是一种非常简单也是使用比较多的一种传值方式,但是它将传递的值显示在浏览器的地址栏中,如果是传递一个或多个安全性要求不高或是结构简单的数 ...
- 用CSS+Jquery实现一个漂浮的窗体
一.项目需求: 实现一个用于网站主页面 从窗体左上角开始飘到右下角 之后又飘到右上角 十秒之后消失的效果. 二.需求分析: 首先 应当想要漂浮的内容放在一个容器内,也就是一个DIV,设计它的样式,不管 ...
- PHP 解决未定义变量报错
在PHP中 有时候会出现 Notice: Undefined index: sid in D:\Apache Group\Apache2\htdocs\php_mobile\mobile\chao\s ...
- Notepad++在编程使用时的小技巧
http://blog.csdn.net/freewaywalker/article/details/8010790 为了编程时更快捷和适应个人习惯,我们可以对Notepad++进行一系列的设置,这当 ...
- [转]Delphi I/O Errors
The following are the Windows API (and former DOS) IO errors, which are also the IO errors often ret ...
- Java如何连接到MySQL数据库的
下载:mysql-connector-java-5.1.38.tar.gz http://dev.mysql.com/downloads/connector/j/ tar zxvf mysql-con ...
- 【SPOJ839】最优标号
[题目描述] 给你一张无向图G(V,E).每个顶点都有一个标号,它是一个[0,2^31-1]内的整数.不同的顶点可能会有相同的标号. 对每条边(u,v),我们定义其费用cost(u,v)为u的标号与v ...
- jQuery慢慢啃之选择器(二)
1.$("#myDiv");ID匹配一个元素 <span id="foo[bar]"></span> $("#foo\\[ba ...
- CetnOS minimal 网络不可用
系统版本: CentOS-6.6-i386-minimal 问题说明: CentOS minimal 在安装完成之后,网络不可用,一些常见的命令报错,如: ping: unknow host xxxy ...