Remove Element

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 you leave beyond the new length.

 

Code:

 1     int removeElement(vector<int>& nums, int val) {

 2         if (!nums.empty() )

 3         {

 4             vector<int>::iterator it = nums.begin();

 5             while (it != nums.end())

 6            {

 7                 if (*it == val)

 8                     it = nums.erase(it);

 9                 else

10                     ++it;

11             }

12         }

13          return nums.size();

14     }

 

你可能感兴趣的:(element)