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 1122 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的更多相关文章

  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 OJ Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. [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 ...

  9. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

随机推荐

  1. iOS-设置状态栏白色以及覆盖状态栏

    iOS-设置状态栏白色以及覆盖状态栏 将状态栏设置为白色 首先, 在info.plist中添加一个标记. View controller–based status bar appearance键值设置 ...

  2. warning:This application is modifying the autolayout engine from a background thread

    警告提示:This application is modifying the autolayout engine from a background thread, which can lead to ...

  3. enter 默认搜索

    onkeydown=" if(event.keyCode==13) Search(); "

  4. 自己写的自动生成动态边框的jquery小插件

    思路就是在元素四周添加<ul>列表,然后周期性地改变它的颜色,实现动态的效果,不支持ie7.ie8 预览链接http://gorey.sinaapp.com/myBorder/border ...

  5. CSS Clip剪切元素实例

    一.实例1: .fixed { position: fixed; width: 382px; height: 100px; background: red; border: 1px solid blu ...

  6. TabHost理解与使用

    一.继承关系 java.lang.Object ↳ android.view.View ↳ android.view.ViewGroup ↳ android.widget.FrameLayout ↳ ...

  7. SQL SERVER 存储过程基础

    一.注释 -- 单行注释,从这到本行结束为注释,类似C++,c#中// /* … */ 多行注释,类似C++,C#中/* … */ 二.变量 (int, smallint, tinyint, deci ...

  8. Orace数据库锁表的处理与总结<摘抄与总结一>

    TM锁(表级锁)类型共有5种,分别称为共享锁(S锁).排它锁(X锁).行级共享锁(RS锁).行级排它锁(RX锁).共享行级排它锁(SRX锁) 当Oracle执行DML语句时,系统自动在所要操作的表上申 ...

  9. phpstrom+xdebug调试PHP代码

    众所周知开发PHP的IDE种类繁多,然而开发PHP并不能像开发其他语言一样,调试PHP代码对诸多新手来说,搭建调试环境就比较麻烦!其实哈,我发现NuSphere-phped-16.0很强大,集成了很强 ...

  10. 2016.7.13abstract

    abstract的使用: 1.当许多类中有相同的功能,功能的内容不同,那么我们向上提取功能的定义. 2当功能的定义被 abstract修饰后,那么它的类也要被abstract修饰,使其抽象化. 3被a ...