remove-element.cpp (775B)
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 //Leet Ratings 6 // Speed Memory 7 //Total 0ms 8.9MB 8 //Beats 100% 47.58% 9 // 10 11 12 //This question is terrible! 13 //This is basically just a side effect where you need 14 //to update the vector and return the number of good 15 //values in it. 16 17 18 int removeElement(vector<int>& nums, int val) { 19 int count = 0; 20 vector<int> num; 21 for(int i = 0 ; i < nums.size() ; ++i){ 22 if(nums[i] != val){ 23 num.push_back(nums[i]); 24 count += 1; 25 } 26 } 27 nums = num; 28 return count; 29 } 30 31 32 int main(){ 33 vector <int> vec = {0, 1, 2, 3, 4, 5, 6,6,6,5,4}; 34 removeElement(vec , 4); 35 for(int i = 0 ; i < vec.size(); ++i){ 36 cout << vec[i] << " "; 37 } 38 cout << endl; 39 40 }