116. Remove the specified element removeSpecifyElement

Question description

Question link

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 array after removal.

Don't use extra array space, you must 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?

Please note that the input array is passed by "reference", which means that modifications to the input array in the function are visible to the caller.

You can imagine the internal operation as follows:

// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
int len = removeSpecifyElement(nums, val);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
    
    
    System.out.print(nums[i] + "\t");
}

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, if the new length returned by the function is 2, and nums = [2,2,3,3] or nums = [2,2,0,0], it will also be regarded as the correct answer.

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,3,0,4]
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

Problem-solving ideas

  1. Create the collection first
  2. Traverse the nums array, determine whether each value is equal to val, and add it if it is not equal.
  3. Because of the ordering of the array itself and the ordering of the collection, the contents of the collection are what we want. Traverse the collection and modify the elements of the array.
  4. Just return the collection size directly.

Detailed code explanation

package question;

import java.util.LinkedList;
import java.util.List;

/**
 * @author keke
 * @version 1.0
 * @className Question116
 * @description
 * @time 2023/5/29 23:33
 */
public class Question116 {
    
    

    public static void main(String[] args) {
    
    
        int[] nums = {
    
    0, 1, 2, 2, 3, 0, 4, 2};
        int val = 2;
        // nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
        int len = removeSpecifyElement(nums, val);

        // 在函数里修改输入数组对于调用者是可见的。
        // 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
        for (int i = 0; i < len; i++) {
    
    
            System.out.print(nums[i] + "\t");
        }
    }

    private static int removeSpecifyElement(int[] nums, int val) {
    
    
        List<Integer> list = new LinkedList<>();
        for (int num : nums) {
    
    
            if (num != val) {
    
    
                list.add(num);
            }
        }
        int i = 0;
        for (Integer integer : list) {
    
    
            nums[i++] = integer;
        }
        return list.size();
    }
}

Run screenshot

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_43344151/article/details/130938792