【Delete Duplicates in Sorted Array II】

Remove duplicates in sorted array II

Give you an ordered array nums, please delete the repeated elements in place , so that the elements that appear more than twice only appear twice **, and return the new length of the deleted array.

Don't use extra array space, you have to modify the input array in -place and do so with O(1) extra space .

illustrate:

Why is the returned value an integer, but the output answer is an array?

Note that the input array is passed by **"reference"**, which means that modifications to the input array within the function are visible to the caller.

You can imagine the internal operation as follows:

// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
    
    
    print(nums[i]);
}

Example 1:

Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3]
Explanation: The function should return the new length length = 5, and the previous array The five elements are modified as 1, 1, 2, 2, 3. Elements beyond the new length in the array do not need to be considered.

Example 2:

Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3]
Explanation: The function should return new The length length = 7, and the first five elements of the original array are modified to 0, 0, 1, 1, 2, 3, 3. Elements beyond the new length in the array do not need to be considered.

hint:

1 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums are sorted in ascending order

Author: LeetCode
Link: https://leetcode.cn/leetbook/read/all-about-array/x9nivs/

int removeDuplicates(vector<int>& nums) {
    
    
    if (nums.size() <= 2) return nums.size();
    int i = 2;
    for (int j = 2; j < nums.size(); j++) {
    
    
        if (nums[j] != nums[i - 2]) {
    
    
            nums[i] = nums[j];
            i++;
        }
    }
    return i;
}

As an example, consider the array nums = [0, 0, 1, 1, 1, 1, 2, 3, 3].

Initial state: i = 2, j = 2 nums = [0, 0, 1, 1, 1, 1, 2, 3, 3]

Traversal process:

  1. The first loop: nums[j] != nums[i - 2], copy nums[j] to nums[i], then add 1 to both i and j. i = 3, j = 3 nums = [0, 0, 1, 1, 1, 1, 2, 3, 3]
  2. The second loop: nums[j] == nums[i - 2], just add 1 to j. j = 4
  3. The third loop: nums[j] == nums[i - 2], just add 1 to j. j = 5
  4. The fourth cycle: nums[j] != nums[i - 2], copy nums[j] to nums[i], and then add 1 to both i and j. i = 4, j = 6 nums = [0, 0, 1, 1, 2, 1, 2, 3, 3]
  5. The fifth cycle: nums[j] != nums[i - 2], copy nums[j] to nums[i], and then add 1 to both i and j. i = 5, j = 7 nums = [0, 0, 1, 1, 2, 3, 2, 3, 3]
  6. The sixth cycle: nums[j] == nums[i - 2], only add 1 to j. j = 8
  7. The seventh cycle: nums[j] != nums[i - 2], copy nums[j] to nums[i], and then add 1 to both i and j. i = 6, j = 9 (break out of the loop because j reaches the end of the array) nums = [0, 0, 1, 1, 2, 3, 3, 3, 3]

The loop ends.

At this point i = 6, the length of the new array is 6. The new array is [0, 0, 1, 1, 2, 3, 3].

Guess you like

Origin blog.csdn.net/u013454780/article/details/130568040