[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 ...
随机推荐
- Java基础知识强化之集合框架笔记40:Set集合之HashSet存储自定义对象并遍历
1. HashSet存储自定义对象并遍历 2. 代码示例: (1)Student类,如下: package cn.itcast_02; /** * @author Administrator * */ ...
- Android(java)学习笔记205:网易新闻RSS客户端应用编写逻辑过程
1.我们的项目需求是编写一个新闻RSS浏览器,RSS(Really Simple Syndication)是一种描述和同步网站内容的格式,是使用最广泛的XML应用.RSS目前广泛用于网上新闻频道,bl ...
- 原创翻译:iOS 应用程序 蓝牙后台运行
默认情况下,普通的CoreBluetooth 任务中的大多数,无论是Central还是peripheral ,在后台或者挂起状况下都是无法进行的.也就是说,你可以通过宣布你的应用程序支持后台处理模式来 ...
- 【Android】android镜像翻转
Android镜像翻转指的是将屏幕进行水平的翻转,达到所有内容显示都会反向的效果,就像是在镜子中看到的界面一样.这种应用的使用场景相对比较受限,主要用在一些需要使用Android手机界面进行镜面投影的 ...
- 程序从高版本降到2.0,数据集报错 TypedTableBase
错误 命名空间“System.Data”中不存在类型或命名空间名称“TypedTableBase”(是缺少程序集引用吗?) 解决: 该错误出现在自动生成的XXX.Designer.cs里. .NET ...
- C#开发基于Http的LaTeX数学公式转换器
本文将讲解如何通过codecogs.com和Google.com提供的API接口来将LaTeX数学函数表达式转化为图片形式.具体思路如下: (1)通过TextBox获取用户输入的LaTeX数学表达式, ...
- POJ 3468.A Simple Problem with Integers 解题报告
用树状数组和线段树会比较简单,这里用这道题来学习Splay. 第一次写,代码比较丑 /* 初始化添加一个key值足够大的结点 保证每个需要的结点都有后继 */ #include <iostrea ...
- 性能测试实践-linux
需求:线上系统性能优化,查找服务器和线上系统瓶颈 根据线上经验数据及期望值定量 数据 up down 线上数据 50 500 测试数据 100 500~2000+ 测试数据 200 500~200 ...
- Java使用百度云存储BCS-让你的数据下载飞起来
作者:Vinkn 来自http://www.cnblogs.com/Vinkn/ 一.简介 云也不是一个新概念了,云到底是什么东西,你叫我说个明明白白的我也说不出来,姑且算作联网的就叫做云.国内的云服 ...
- 前端模板文件化jQuery插件 $.loadTemplates
工作中使用前端模板引擎,如 artTemplate.jsRender,来替代拼接字符串. 可是直接把模板写在页面上会带来页面臃肿,模板无法重用,与 ASP.NET等后端语言语法冲突等问题. 所以将多个 ...