[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语言基础(二)
Java语言基础(二) 一.变量续 (1).变量有明确的类型 (2).变量必须有声明,初始化以后才能使用 (3).变量有作用域,离开作用域后自动回收 变量作用域在块内有效 (4).在同一定义域中变量不 ...
- unlocker208安装之后看不到Apple macos选项,解决办法.
前段时间升级了win10,最新的unlocker208安装以后看不到mac os的选项,为什么呢?我们在管理员允许win-install.cmd的过程中,会在cmd中看到这么一句话:LookupErr ...
- jsp中Java代码中怎么获取jsp页面元素
举例,页面元素<td><input value="${sl }" type="text" id="sl" name=& ...
- JS调用android逻辑方法
1.安卓打开webview时做如下配置 并做一回调接口 这里注意的是 参数 FULIBANG 和 回调接口方法 jsCallWebView 一会在JS里会用到 ================= ...
- eclipse中更改默认编码格式
更改过程如下: (1)window->preferences->general->content Types, 选中java class file修改default encoding ...
- 用PHP实现一个高效安全的ftp服务器(二)
接前文. 1.实现用户类CUser. 用户的存储采用文本形式,将用户数组进行json编码. 用户文件格式: * array( * 'user1' => array( * 'pass'=>' ...
- 【转载】一步一步搭建自己的iOS网络请求库
一步一步搭建自己的iOS网络请求库(一) 大家好,我是LastDay,很久没有写博客了,这周会分享一个的HTTP请求库的编写经验. 简单的介绍 介绍一下,NSURLSession是iOS7中新的网络接 ...
- FMDB多线程读写问题,使用FMDataBaseQueue操作可以解决同时打开一个链接de读写问题
现在ios里使用的数据库一般都是Sqlite,但是使用Sqlite有个不太好的地方就是在多线程的时候,会出现问题,sqlite只能打开一个读或者写连结.这样的话多线程就会碰到资源占用的问题. 最开始是 ...
- JavaScript中style.left与offsetLeft的区别
今天在制作焦点轮播图的时候,遇到一个问题,在使用style.left获取图片的位置时,怎么也获取不到.换用offsetLeft就能够成功获取到了.虽然实现了我想要的效果,但是还是不甘心啊,没有找到原因 ...
- 计算机网络基础_01IP地址
1,IP地址组成和分级分级 IP地址=网络地址+主机地址 32位,4段组成 A:最高位是0 ,1个字节的网络地址,3个字节的主机地址 B:最高位是10,2个字节的网络地址,2个字节的主机地址 C:最高 ...