【LeetCode 面试经典150题】80. Remove Duplicates from Sorted Array II 在有序数组中移除重复元素II

80. Remove Duplicates from Sorted Array II

题目大意

Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

中文释义

给定一个按非递减顺序排序的整数数组 nums,就地删除一些重复项,使每个唯一元素最多出现两次。元素的相对顺序应保持不变。

由于在某些语言中无法改变数组的长度,你必须将结果放在数组 nums 的前部。更具体地说,如果删除重复项后有 k 个元素,那么 nums 的前 k 个元素应该包含最终结果。超出前 k 个元素的部分不重要。

在将最终结果放置在 nums 的前 k 个槽位后,返回 k

不要为另一个数组分配额外空间。你必须通过就地修改输入数组并且仅使用 O(1) 的额外内存来完成此操作。

示例

Example 1:

  • Input: nums = [1,1,1,2,2,3]
  • Output: 5, nums = [1,1,2,2,3,_]
  • Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

  • Input: nums = [0,0,1,1,1,1,2,3,3]
  • Output: 7, nums = [0,0,1,1,2,3,3,_,_]
  • Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints:

  • 1 <= nums.length <= 3 * 10^4
  • -10^4 <= nums[i] <= 10^4
  • nums is sorted in non-decreasing order.

解题思路1

算法描述

代码实现了从排序数组中移除多余的重复项,确保每个元素最多出现两次。

  1. 初始化变量:

    • index: 用于跟踪数组中当前非重复元素的位置的指针。
    • cnt_index: 用于记录当前 index 指向的元素在数组中出现的次数的计数器。
  2. 遍历数组:

    • 使用 for 循环从数组的第二个元素(索引 1)开始遍历。
  3. 处理元素:

    • cnt_index < 2(即当前 index 指向的元素出现次数少于2次)时,将当前遍历到的元素 (nums[i]) 移动到 index + 1 的位置,并更新 cnt_index。使用条件运算符判断是否增加 cnt_index
    • cnt_index 等于2,并且当前元素与 index 指向的元素不同时,将当前元素移动到 index + 1 的位置,并将 cnt_index 重置为1。
  4. 返回结果:

    • 返回 index + 1 作为新数组的长度,这表示新数组中元素的实际数量。

复杂度分析

  • 时间复杂度: O(n),其中 n 是数组 nums 的长度。需要遍历一次数组来移除多余的重复项。
  • 空间复杂度: O(1),只使用了有限的额外空间,即在原地修改了数组。
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        // 降序数组,原地移除出现次数超过2次的元素。
        // 设置 cnt_index 记录当前index下标的元素出现的次数
        int index = 0, cnt_index = 1;
        for (int i = 1; i < nums.size(); i++) {
            if (cnt_index < 2) {
                nums[++index] = nums[i];
                cnt_index = nums[index - 1] == nums[index] ? cnt_index + 1 : cnt_index;
            } else if (nums[index] != nums[i]) {
                nums[++index] = nums[i];
                cnt_index = 1;
            }
        }
        return index + 1;
    }
};

解题思路2

算法描述

优化后的代码进一步简化了移除数组中多余重复项的逻辑,确保每个元素最多出现两次。

  1. 特殊情况处理:

    • 如果数组长度小于3,直接返回数组长度,因为在这种情况下,不可能有超过两次的重复。
  2. 初始化变量:

    • index: 设置为1,因为前两个元素(即索引为0和1的元素)默认保留。
  3. 遍历并更新数组:

    • 从第三个元素(索引为2)开始遍历数组。
    • 如果当前元素 nums[i] 不等于 nums[index] 或者不等于 nums[index - 1],则递增 index 并更新 nums[index]。这样确保每个元素最多只出现两次。
  4. 返回结果:

    • 返回 index + 1 作为新数组的长度,代表去除多余重复项后的数组长度。

代码示例

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if (nums.size() < 3) return nums.size();

        int index = 1;
        for (int i = 2; i < nums.size(); i++) {
            if (nums[i] != nums[index] || nums[i] != nums[index - 1]) {
                nums[++index] = nums[i];
            }
        }
        return index + 1;
    }
};
  • 25
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值