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 ☆(从有序数组中删除重复项)

 

相关文章:

  • 2022-12-23
  • 2022-02-19
  • 2021-08-25
  • 2021-09-10
  • 2021-08-15
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-06
  • 2021-08-21
  • 2022-01-17
  • 2022-02-26
  • 2021-06-08
  • 2022-02-14
  • 2020-02-17
相关资源
相似解决方案