The Remove element is a water problem for leetcode, but there are a number of ways to do this, and now I'm going to do a little discussion about some of the things I know.
Title Link: https://leetcode.com/problems/remove-element/
Title Description: Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn ' t matter what are you leave beyond the new length.
It is important to note that "It doesn ' t matter what do you leave beyond the new length."
Thought analysis: The idea is very simple, directly on the code.
Code One: Violent movement
1 classSolution {2 Public:3 intRemoveelement (vector<int>& Nums,intval) {4 intLen =nums.size ();5 intPTR =0;6 intNewlen =0;7 for(inti =0; i<len;i++)8 {9 if(nums[i]!=val)Ten { Onenums[ptr++]=Nums[i]; A } - } -Newlen =ptr; the returnNewlen; - } -};
Code two: Using STL
1 classSolution {2 Public:3 intRemoveelement (vector<int>& Nums,intval) {4Auto end = Remove (Nums.begin (), Nums.end (), Val);//The automatic pointer is used here.5 returndistance (Nums.begin (), end);6 }7};
[Leetcode] Remove Element Analysis