[Algorithm] Remove the specified element in the array in place

topic:

Given an array nums and a value val, you need to remove all elements whose value is equal to val in place, and return the new length of the removed array.

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

The order of elements can be changed. You don't need to consider elements in the array beyond the new length.

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 is passed by "reference". That is, do not make any copies of the actual parameters int len ​​= removeElement(nums, val);

// Modifications to the input array in the function are visible to the caller. // Depending on the length returned by your function, it will print out all elements in the array within that length. for
(int i = 0; i < len; i++) { print(nums[i]); }

Example 1:

Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2] Explanation: The function should return the new length 2, and the
first two elements in nums are both 2. You don't need to consider elements in the array beyond the new length. For example, the new length returned by the function is 2, and nums =
[2,2,3,3] or nums = [2,2,0,0], will also be considered as the correct answer.

Example 2 :

Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, nums = [0,1,4,0,3]
Explanation: The function should return the new length 5, and the first five elements in nums are 0, 1, 3, 0,
4. Note that these five elements can be in any order. You don't need to consider elements in the array beyond the new length.

hint:

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

Source: LeetCode

Problem-solving ideas:

1. Cycle delete

1. Use a loop to find the target element in the loop, and then delete it.
2. After deletion, because the original array is missing one bit, the index will start looping directly from the lower position of the deleted element, and one bit will be missed, so it has to be rolled back by one. bit

/**
 * @param {number[]} nums
 * @param {number} val
 * @return {number}
 */
var removeElement = function(nums, val) {
    
    
    for(let i = 0; i < nums.length; i++)  {
    
    
        if (nums[i] === val) {
    
    
            nums.splice(i, 1);
            i--; // 重要的是这句,因为删除前Index已经发生变化,删除后直接从index+2开始循环,所以要回退一位重新开始循环
        }
    }
    return nums.length
};

insert image description here

2. Double pointer

To sum it up,
1. Use two pointers at the left and right heads to traverse the array from both ends to the center.
2. If it is inconsistent with the target element, move the left pointer to the right.
3. If it is consistent with the target element, copy the element before the right pointer to At the current position, move the right pointer and replace it with the duplicate element to achieve the effect of deleting the element (but not really delete it). 4.
When the left and right pointers coincide, return to the position of the left pointer, which is the position of the array element after deduplication.
Here is the step-by-step diagram
For example, our original The array is [2,3,3,5,1] and the final result is [2,1,5,5,1]. We can return the length of [2, 5, 1]

var removeElement = function(nums, val) {
    
    
	let left = 0, right = nums.length;
	while(left < right) {
    
    
		if(nums[left] === val) {
    
    
			nums[left] = nums[right - 1]
			right--;
		} else {
    
    
			left++;
		}
	}
	return left
}

insert image description here

Guess you like

Origin blog.csdn.net/weixin_38318244/article/details/127241848