This is the 20th day of my participation in the August Wen Challenge.More challenges in August

Topic describes

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

Instead of using extra array space, you must only use O(1) extra space and modify the input array in place.

The order of elements can be changed. You don’t need to worry about the element after the new length in the array.

 

Description:

Why is the return value an integer, but the output answer is an array?

Note that the input array is passed “by reference,” which means that modifying the input array in a function is visible to the caller.

You can imagine the internal operation as follows:

// Nums is passed by reference. Int len = removeElement(nums, val); // Modifying the input array in a function is visible to the caller. // Depending on the length returned by your function, it prints out all elements in the array within that length. for (int i = 0; i < len; i++) { print(nums[i]); }Copy the code

The sample

Example 1: Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2]

Explanation: The function should return a new length of 2, and the first two elements in nums are both 2. You don’t need to worry about the element after the new length in the array. For example, the function returns a new length of 2, and nums = [2,2,3,3] or nums = [2,2,0,0] will also be considered the correct answer.

Example 2: input: nums =,1,2,2,3,0,4,2 [0], val = 2 output: 5, nums =,1,4,0,3 [0]

Explanation: The function should return a new length of 5, and the first five elements in nums are 0, 1, 3, 0, 4. Notice that these five elements can be in any order. You don’t need to worry about the element after the new length in the array.

Tip:

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

Thought analysis

This problem follows a daily algorithm to remove duplicates from an ordered array (juejin. Cn) using “double Pointers”.

One is the function of the cursor, which is responsible for traversing the entire array, and the other is the function of the locator. If there is an equal value, it indicates that the element that the locator points to is repeated, and the locator does not move, waiting for the cursor to find the number that does not repeat to fill in.

But different from the previous problem, here the locator and the cursor in the encounter with the target value is not the same when the synchronization, until the encounter with the target value is the same, only the cursor plus one. So instead of index+1, we’re going to return index.

Code implementation

class Solution {
    public int removeElement(int[] nums, int val) {
        int index = 0;
        if(nums.length == 0){
            return 0;
        }
        for(int i = 0; i < nums.length; i++){
            if(nums[i] != val){
                nums[index++] = nums[i];
            }
        }
        return index;
    }
}
Copy the code

The last

27. Remove elements – LeetCode (leetcode-cn.com)

Welcome to provide new ideas and methods!