码迷,mamicode.com
首页 > 其他好文 > 详细

Remove Element

时间:2015-07-09 00:24:08      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/riden/p/4631527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!