design algorithm, in the array r[n] delete all elements whose element value is x, requiring time complexity of O(n); The spatial complexity is O(1).
#include<iostream>
using namespace std;
int Deletex(int r[],int n,int x)
{
int j=0,k=0;
for(int i=0;i<n;i++)
{
if(r[i]!=x)
{
r[j]=r[i];
j++;
}
else
k++;
}
return k;
}
int main()
{
int r[1000];
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>r[i];
int x; //输入要删除的元素
cin>>x;
int m=Deletex(r,n,x);
for(int i=0;i<n-m-1;i++)
cout<<r[i]<<" ";
cout<<r[n-m-1];
return 0;
}
div>
Read More:
- Array of PHP_ diff,array_ intersect,array_ merge, in_ Is there a limit on the number of arrays in array?
- valueError: Length mismatch: Expected axis has 40 elements, new values have 38 elements
- [solved] error: valueerror: expected 2D array, got scalar array instead
- Vector delete pop of element_ back(),erase(),remove()
- Two dimensional array and pointer to one dimensional array
- Variable type error: error of array occurs when TP5 controller receives the array of post
- V-for styles individual elements
- Python uses the priority queue to get the maximum k elements
- Selenium: 8 Ways to Locate Elements
- Solve the unmarshalling error: unexpected element (URI: “local:” name “). Expected elements are
- Python random selects elements randomly from a collection
- Lua — using remove to delete table data
- Several ways to center elements horizontally and vertically
- How to use C + + function pointer array
- JQuery is a solution to the disappearance of listening events after adding elements with append
- ArcGIS import database elements error 000210
- Leetcode 34 finds the first and last position of an element in the sorted array (medium)
- Array initializer is not allowed here
- JavaScript removes the number specified in the array
- Implement Set using Array.