[Delete duplicates in sorted array]

Remove duplicates in sorted array

Given an array in ascending order nums, 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. Then returns the number of unique elements numsin .

Considering numsthat the number of unique elements is k, you need to do the following to ensure that your solution will pass:

  • Changes the array numsso numsthat kthe first elements of contain unique elements, in the order they originally appeared numsin . numsThe size of the remaining elements numsof is not important.
  • return k.

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:

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

Example 2:

Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4]
Explanation: The function should return the new length 5, and the first five elements of the original array nums are modified to 0, 1, 2, 3, 4. 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

This is one 双指针问题. To solve this problem, we can use two pointers, a slow pointer j iand a fast pointer j. The slow pointer iis used to point to the last position of the current non-repeated element, and the fast pointer jis used to traverse the array. During the traversal process, if the element pointed to jby iis different from the element pointed to by the slow pointer, it means that a new non-repeating element is encountered, and we will put the new non-repeating element in the next position iof Slow hand Moves one position ibackward . When the traversal ends, the position iof plus 1is the length of the new array.

Follow the steps below:

  1. Initializes two pointers iand j, both pointing to the first element of the array.

  2. Traverse the array until the fast pointer jreaches the end of the array.
    a. If nums[i] != nums[j], means that a new non-repeating element is encountered, nums[j]copy to it nums[i+1], and then move the slow pointer ibackward by one position.

    b. Move the fast pointer jback one position.

  3. After the traversal, return i+1, which is the length of the new array.

int removeDuplicates(vector<int>& nums) {
    
    
    if (nums.size() == 0) return 0;  // 如果数组为空,直接返回 0
    int i = 0;  // 定义慢指针 i,初始时指向数组的第一个元素
    for (int j = 1; j < nums.size(); j++) {
    
      // 定义快指针 j,从数组的第二个元素开始遍历
        if (nums[i] != nums[j]) {
    
      // 如果快指针 j 指向的元素与慢指针 i 指向的元素不同
            i++;  // 将慢指针 i 向后移动一个位置
            nums[i] = nums[j];  // 将快指针 j 指向的元素复制到慢指针 i 的下一个位置
        }
    }
    return i + 1;  // 返回新数组的长度
}
int removeDuplicates(vector<int>& nums) {
    
    
    if (nums.size() == 0) return 0;  // 如果数组为空,直接返回 0
    int i = 0, j = 1;  // 定义慢指针 i 和快指针 j,初始时分别指向数组的第一个元素和第二个元素
    while (j < nums.size()) {
    
      // 只要快指针 j 没有到达数组末尾
        if (nums[i] != nums[j]) {
    
      // 如果快指针 j 指向的元素与慢指针 i 指向的元素不同
            i++;  // 将慢指针 i 向后移动一个位置
            nums[i] = nums[j];  // 将快指针 j 指向的元素复制到慢指针 i 的下一个位置
        }
        j++;  // 将快指针 j 向后移动一个位置
    }
    return i + 1;  // 返回新数组的长度
}

Detailed process steps:

  1. Check the array length and return 0 if it is 0.

  2. Initialize two pointers: slow pointer i and fast pointer j. i points to the first element of the array and j points to the second element of the array.

  3. Traverse the array until the fast pointer j reaches the end of the array.

    a. Determine whether nums[i] is not equal to nums[j].

    b. If nums[i] is not equal to nums[j], it means that a new unique element is encountered.

    ​ i. Move the slow pointer i backward one position.

    ii. Copy nums[j] to nums[i].

    c. If nums[i] is equal to nums[j], do nothing.

    d. Move the fast pointer j backward one position.

  4. When the traversal is over, return i + 1, which is the length of the new array.

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

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

Traversal process:

  1. First loop: nums[i] == nums[j], do nothing, j = 2.
  2. Second loop:nums[i] != nums[j],i = 1,nums[i] = nums[j],j = 3。 nums = [0, 1, 1, 1, 1, 2, 2, 3, 3, 4]
  3. The third cycle: nums[i] == nums[j], no operation is performed, j = 4.
  4. The fourth cycle: nums[i] == nums[j], no operation is performed, j = 5.
  5. Fifth cycle:nums[i] != nums[j],i = 2,nums[i] = nums[j],j = 6。 nums = [0, 1, 2, 1, 1, 2, 2, 3, 3, 4]
  6. The sixth cycle: nums[i] == nums[j], no operation is performed, j = 7.
  7. Seventh cycle:nums[i] != nums[j],i = 3,nums[i] = nums[j],j = 8。 nums = [0, 1, 2, 3, 1, 2, 2, 3, 3, 4]
  8. The eighth cycle: nums[i] == nums[j], no operation is performed, j = 9.
  9. Ninth loop: nums[i] != nums[j],i = 4,nums[i] = nums[j],j = 10(Break out of the loop because j reaches the end of the array).nums = [0, 1, 2, 3, 4, 2, 2, 3, 3, 4]

The loop ends.

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

Guess you like

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