Remove Element

Question:

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.

Solution:

 1 class Solution {
 2 public:
 3     int removeElement(vector<int>& nums, int val) {
 4     vector<int>::iterator index=nums.begin();
 5     int count=0;
 6     for(vector<int>::iterator iter=nums.begin();iter!=nums.end();iter++)
 7     {
 8         if(*iter!=val)
 9         {*index++=*iter;count++;}            
10     }
11     return count;
12         
13     }
14 };

Remove Element_第1张图片

你可能感兴趣的:(element)