Title Description:
Given an array and a value, delete the same number as the value in situ, returning the length of the new array.
The idea of violence in this topic is to traverse through a pointer, and delete which element will move the elements forward one position. But the complexity of this time is so great that you may want to move a lot of elements.
Here is the idea of dividing in a quick sort: with two pointers, the latter pointer is used for traversal, the previous pointer as a tag, indicating that the element before the tag satisfies the requirements .
So the specific code:
int removeelement (vector<int> &arr, int elem) {//write your code hereint size = Arr.size (); if (size = = 0) {return 0;} int pre = -1;for (int cur = 0; cur < size; ++cur) {if (Arr[cur]! = elem) {++pre;if (cur! = Pre) {swap (Arr[pre], arr[cur]);}} If}//forreturn pre + 1;}
Delete all elements in an array equal to a key