3. Remove duplicates from an ordered array

3. Remove duplicates from an ordered array

给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,
返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回 nums 中唯一元素的个数。

Considering that the number of unique elements of nums is k, you need to do the following to ensure that your solution can be passed:

  • Change the array nums so that the first k elements of nums contain unique elements in the order in which they originally appeared in nums. The remaining elements of nums are unimportant to the size of nums.
  • Return k.

Judgment criteria:

The system will use the following code to test your solution:

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, your solution will be passed.

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 is sorted in ascending order

Solution 1

Method 1: The requirement of this question on double pointers
is: delete duplicate elements from the given ordered array nums. After deleting the duplicate elements, each element only appears once and returns the new length. The above operation must be modified in place. The array method is completed with O(1) space complexity.

Since the given array nums is ordered, for any i<j, if nums[i]=nums[j], then for any i≤k≤j, there must be nums[i]=nums[k]= nums[j], that is, the subscripts of equal elements in the array must be consecutive. Taking advantage of the ordered characteristics of the array, duplicate elements can be deleted through the double pointer method.

If the length of array nums is 0, then 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 at least one element remains after deleting the duplicate elements. Therefore, nums[0] can remain as it is, and duplicate elements will be deleted starting from subscript 1.

Define two pointers, fast and slow, as fast pointers and slow pointers respectively. The fast pointer represents the subscript position reached by traversing the array, and the slow pointer represents the subscript position to be filled in with the next different element. Initially, both pointers point to the subscript. 1.

Assume that the length of the array nums is n. The fast pointer fast traverses each position from 1 to n−1 in sequence. For each position, if nums[fast] ≠nums[fast−1], it means that nums[fast] is different from the previous elements, so nums The value of [fast] is copied to nums[slow], and then the value of slow is increased by 1, which points to the next position.

After the traversal is completed, each element from nums[0] to nums[slow−1] is different and contains every different element in the original array, so the new length is slow, and just return slow.

Code:

class Solution {
    
    
    public int removeDuplicates(int[] nums) {
    
    
        int n = nums.length;
        if (n == 0) {
    
    
            return 0;
        }
        int fast = 1, slow = 1;
        while (fast < n) {
    
    
            if (nums[fast] != nums[fast - 1]) {
    
    
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }
}

Guess you like

Origin blog.csdn.net/weixin_44796239/article/details/130595710