[Data Structure and Algorithm Series 2] Remove elements in place

Given an array numsand a value val, you need to in-place remove all valelements whose value is equal to 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.

Example 1:

输入:nums = [3,2,2,3], val = 3
输出:2, nums = [2,2]
解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。

Example 2:

输入:nums = [0,1,2,2,3,0,4,2], val = 2
输出:5, nums = [0,1,4,0,3]
解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。

Hint: use double pointer arithmetic.

Double pointer method (fast and slow pointer method): complete the work of two for loops under one for loop through a fast pointer and a slow pointer.

Define fast and slow pointers

  • Fast pointer: Find the elements of the new array, the new array is the array that does not contain the target element
  • Slow pointer: point to the location where the subscript of the new array is updated

C++Code:

#include <iostream>
#include <vector>
#include "array_algorithm.h"
using namespace std;

//暴力解算
int removeElement_violent(vector<int>& nums, int value) {
	int nums_size = nums.size();
	for (int i = 0; i < nums_size; i++) {
		if (nums[i] == value) {
			for (int j = i + 1; j < nums_size; j++) {
				nums[j - 1] = nums[j];
			}
			i--;
			nums_size--;
		}
	}
	return nums_size;
}

// 双指针解算
int removeElement_fasterslowpoint(vector<int>& nums, int value) {
	int nums_size = nums.size();
	int slowpoint = 0;
	for (int fasterpoint = 0; fasterpoint < nums_size; fasterpoint++) {
		if (nums[fasterpoint] != value) {
			nums[slowpoint++] = nums[fasterpoint];
		}
	}
	return slowpoint;
}

insert image description here

insert image description here

pythonCode:

from typing import List


# 偷懒解法
def removeElement(nums: List[int], value: int):
    nums_size = len(nums)

    for i in nums:
        if i == value:
            nums.remove(i)

    return len(nums)


# 暴力解法
def removeElement_violent(nums: List[int], value: int) -> int:
    i, nums_len = 0, nums_len(nums)
    while i < nums_len:
        if nums[i] == value:
            for j in range(i + 1, nums_len):
                nums[j - 1] = nums[j]
            nums_len -= 1
            i -= 1
        i += 1
    return nums_len


# 双指针解法
def removeelement_fasterslowpoint(nums: List[int], value: int) -> int:
    fastpoint = 0  
    slowpoint = 0  
    nums_size = len(nums)
    while fastpoint < nums_size: 
        if nums[fastpoint] != value:
            nums[slowpoint] = nums[fastpoint]
            slowpoint += 1
        fastpoint += 1
    return slowpoint


if __name__ == '__main__':
    nums = [1, 2, 3, 4, 5, 9, 10, 12]
    value = 9
    print(f"len of nums after remove element:{
      
      removeElement(nums, value)}")
    print(f"len of nums after remove_violent element:{
      
      removeElement_violent(nums, value)}")
    print(f"len of nums after remove_fasterslowpoint element:{
      
      removeelement_fasterslowpoint(nums, value)}")

insert image description here

Guess you like

Origin blog.csdn.net/qq_43456016/article/details/132660835