[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

Posted fanguangdexiaoyuer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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%2B%2B-Java-Python-Ruby

描述

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

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].

解析

有序数组,如果要删除值,必须当前位置的值 > 当前位置-2的值.

代码

public int removeDuplicates(int[] nums) {
    int i = 0;
    for (int n : nums)
        if (i < 2 || n > nums[i-2])
            nums[i++] = n;
    return i;
}

类似问题:[LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

 

以上是关于[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)的主要内容,如果未能解决你的问题,请参考以下文章

leetcode [80]Remove Duplicates from Sorted Array II

leetcode 80 Remove Duplicates from Sorted Array II ----- java

LeetCode OJ 80. Remove Duplicates from Sorted Array II

leetcode80. Remove Duplicates from Sorted List II

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

leetcode 26. Remove Duplicates from Sorted Array 80. Remove Duplicates from Sorted Array II