【LeetCode】-26. Delete duplicates in ordered array

1. Question

  26. Remove duplicates in ordered array - LeetCode

Given an array nums arranged in ascending order, please delete the repeated elements in place so that each element appears only once, and return the new length of the array after deletion. The relative order of elements should remain consistent.

Since the length of an array cannot be changed in some languages, the result must be placed in the first part of the array nums. More formally, 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 must modify the input array in-place and do it using O(1) extra space.

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];
}

2. Example

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

3. Analysis

logic

When comparing elements in an array, the first thing that comes to mind is the fast and slow pointers. There are two situations:

1. The array is empty or the array has only one element

2. If the number of array elements is >2, you need to use fast and slow pointers for comparison. Let the slow pointer point to the first element first, and let the fast pointer point to the second element. For comparison, there are two situations:

(1) If they are equal, then the slow pointer does not need to move, only the fast pointer moves

(2) If they are not equal, then the slow pointer +1, and the element at the slow pointer position becomes the element at the fast pointer position, and then the fast pointer +1

return

Slow initially points to the first element, so the value of slow is initially the subscript 0 of the first element, so when returning, slow must be +1.

4. Code implementation

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        
        //情况一:数组为空或只有一个元素,都不可能存在重复元素
        if(nums.size() == 0 || nums.size() == 1)
        {
            return nums.size();
        }

        //情况二:数组元素个数>2,需要比较
        //思路:使用快慢指针,让慢指针先指向第一个元素,让快指针指向第二个元素,进行比较:
        //(1)若相等,那么慢指针不用动,只移动快指针
        //(2)若不相等,那么慢指针+1,且慢指针位置的元素变成快指针位置的元素,再让快指针+1
        int fast = 1;
        int slow = 0;

        while(fast < nums.size())
        {
            if(nums[fast] == nums[slow])//相等
            {
                fast++;
            }
            else//不相等
            {
                slow++;
                nums[slow] = nums[fast];
                fast++;     
            }
        }

        return slow+1;           
    }
};

Guess you like

Origin blog.csdn.net/gx714433461/article/details/129665381