LeetCode Daily Question: Delete Duplicates in Sorted Array

Remove duplicates in sorted array

Given an array nums in ascending order, please delete repeated elements in place so that each element appears only once, and return the new length of the deleted array. The relative order of elements should be consistent.
Since the length of the array cannot be changed in some languages, the result must be placed in the first part of the array nums. More canonically, if there are k elements after removing duplicates, then the first k elements of nums should hold the final result.
Returns k after inserting the final result into the first k positions of nums.
Instead of using extra space, you have to modify the input array in-place and do so using O(1) extra space.

Judgment criteria:

The system will test your solution with the following code:

int[] nums = [...]; // 输入数组
int[] expectedNums = [...]; // 长度正确的期望答案

int k = removeDuplicates(nums); // 调用

assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
    
    
    assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will pass.

Example 1:

输入:nums = [1,1,2]
输出:2, nums = [1,2,_]
解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。

Example 2:

输入:nums = [0,0,1,1,1,2,2,3,3,4]
输出:5, nums = [0,1,2,3,4]
解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。

hint:


    1 <= nums.length <= 3 * 104
    -104 <= nums[i] <= 104
    nums 已按 升序 排列

Brute force solution

Directly traverse the nums array, assign the non-repeated items in it to a newly created array new[ ], use a marker k to record the number of elements assigned to the new array new[ ] from the nums array, and use it as the return value. Use the index null value to insert the new element into the position in new[], and ensure that it is inserted according to the subscript 0, 1, 2, .... Because of the requirements of the title, it is necessary to delete repeated elements in place so that each element appears only once, and return the new length of the deleted array. The relative order of the elements should be consistent, so after the traversal, reassign the elements in new[ ] to nums[ ], then the first k elements of nums[ ] are all non-repeated items of the original data.
If the length of the array nums[ ] is 0, the array contains no elements, so 0 is returned.
When the length of the array nums[ ] is greater than 0, the array contains at least one element, and because nums[ ] is ordered, the new array new[0]=nums[0].

class Solution {
    
    
    public int removeDuplicates(int[] nums) {
    
    
        //如果数组为空则返回0
        if(nums == null || nums.length == 0)
            return 0;
        int[] b = new int[nums.length];
        b[0] = nums[0];
        int index = 1;
        //删除重复项,如果当前的数字和b[index]不相等,则说明nums[i]是非重复项
        for(int i = 1; i < b.length; i++){
    
    
            if(nums[i] != b[index - 1]){
    
    
                b[index] = nums[i];
                index++;
            }
        }

        for(int i = 0; i < b.length; i++){
    
    
            nums[i] = b[i];
        }
        return index;
    }
}

speed pointer

Define two pointers fast and slow, which are fast pointer and slow pointer respectively. The fast pointer indicates the subscript position reached by traversing the array, and the slow pointer indicates the subscript position to be filled in the next different element. Initially, both pointers point to subscript 1. The slow pointer is similar to the index in Solution 1. They both control the position of the inserted element, and ensure that it is inserted according to the subscript 0, 1, 2, .... If
nums[fast]!=nums[fast-1], it will be nums[fast] is inserted into the position of nums[slow], and then slow++.
After the traversal, each element from nums[0] to nums[slow−1] is different and contains each different element in the original array, so the new length is slow, and returns slow

 public static int removeDuplicates(int[] nums) {
    
    
 //如果数组 nums[ ] 的长度为 0,则数组不包含任何元素,因此返回 0
        if(nums == null || nums.length == 0){
    
    
            return 0;
        }
        int slow=1;//慢指针
        int fast=1;//快指针
        //快指针从下标1开始遍历
        for(; fast < nums.length ; fast++){
    
    
        //如果一个数和它前一个不相等,就认为是非重复项
            if(nums[fast] != nums[fast-1]){
    
    
            //将非重复项插入到slow指针标记的位置
                nums[slow] = nums[fast];
                //slow指针右移一位
                slow++;
            }
        }
      return slow;
    }

Guess you like

Origin blog.csdn.net/Redamancy06/article/details/128309280