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 };

posted on 2015-07-08 22:18  Riden  阅读(138)  评论(0编辑  收藏  举报

导航