首页 > 其他 > 详细

[LeetCode]Remove Element

时间:2015-09-14 00:16:20      阅读:313      评论:0      收藏:0      [点我收藏+]

Remove Element

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.

 

题意:从数组中去除某个值,返回新数组的长度;注意在去除完毕后数组中的元素应该变化了,即出现value的位置都被替换掉了,最后一句的意思是 新长度后面的元素值任意,也就是说,新数组的总长度可以保持不变,但是新长度之前的元素要正确,后面的无所谓。

 1 class Solution {
 2 public:
 3     int removeElement(vector<int>& nums, int val) {
 4         int result = 0;
 5         for(int i=0;i<nums.size();i++)
 6         {
 7             if(nums[i]!=val)
 8             {
 9                 nums[result]=nums[i];
10                 result++;
11             }
12         }
13         return result;
14     }
15 };

 

[LeetCode]Remove Element

原文:http://www.cnblogs.com/Sean-le/p/4805907.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!