[LeetCode]Remove Element

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 };
原文地址:https://www.cnblogs.com/Sean-le/p/4805907.html