【Remove element】

remove element

Given an array numsand a value val, you need to remove all elements valwhose , and return the new length of the removed array.

Do not use 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 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 as a method, 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 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
int len = removeElement(nums, val);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
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

double pointer method

class Solution {
    
    
public:
    int removeElement(vector<int>& nums, int val) {
    
    
        // 定义两个指针 i 和 j,初始时都指向数组的开头
        int i = 0, j = 0;
        // 只要指针 j 没有到达数组的末尾,就继续循环
        while (j < nums.size()) {
    
    
            // 如果当前元素不等于 val,就将它赋值给指针 i 所指向的位置,并将指针 i 向后移动一个位置
            if (nums[j] != val) {
    
    
                nums[i] = nums[j];
                i++;
            }
            // 不管当前元素是否等于 val,都要将指针 j 向后移动一个位置
            j++;
            for (int k = 0; k < nums.size(); k++) {
    
    
                cout << nums[k] << " ";
            }
            cout << endl;
        }
        // 循环结束后,指针 i 所指向的位置就是新数组的长度
        return i;
    }
};

enter

[0,1,2,2,3,0,4,2]
2

output

[0,1,3,0,4]

expected outcome

[0,1,4,0,3]

stdout

0 1 2 2 3 0 4 2
0 1 2 2 3 0 4 2
0 1 2 2 3 0 4 2
0 1 2 2 3 0 4 2
0 1 3 2 3 0 4 2
0 1 3 0 3 0 4 2
0 1 3 0 4 0 4 2
0 1 3 0 4 0 4 2

Analyze the process of array changes in detail.

Initial state:

0 1 2 2 3 0 4 2
i
j

Cycle process:

  1. First loop:nums[i] = nums[j]; i++; j++;
0 1 2 2 3 0 4 2
  i
  j

循环后的数组:[0, 1, 2, 2, 3, 0, 4, 2]
  1. Second loop:; j++;
0 1 2 2 3 0 4 2
    i
    j

循环后的数组:[0, 1, 2, 2, 3, 0, 4, 2]
  1. Third loop:j++
0 1 2 2 3 0 4 2
    i
      j

循环后的数组:[0, 1, 2, 2, 3, 0, 4, 2]
  1. Fourth loop:nums[i] = nums[j]; i++; j++;
0 1 2 2 3 0 4 2
    i
        j

循环后的数组:[0, 1, 2, 2, 3, 0, 4, 2]
  1. Fifth cycle:nums[i] = nums[j]; i++; j++;
0 1 3 2 3 0 4 2
      i
          j

循环后的数组:[0, 1, 3, 0, 3, 0, 4, 2]
  1. Sixth cycle:nums[i] = nums[j]; i++; j++;
  0 1 3 0 3 0 4 2
          i
              j
  
  循环后的数组:[0, 1, 3, 0, 3, 0, 4, 2]
  

7. The seventh cycle:nums[i] = nums[j]; i++; j++; j < nums.size()结束循环

0 1 3 0 4 0 4 2
          i
              j

循环后的数组:[0, 1, 3, 0, 4, 0, 4, 2]

The loop ends. At this point, iit points to the length of the new array, which is 5. The new array is 0 1 3 0 4.

Guess you like

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