leetcode removeElement

27.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.

简单解释来说就是将容器中指定的数移出容器

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        unsigned int tag = 0;
        auto len = nums.size();
        for (int i = 0; i < len; ++i){
            if (nums[i] != val && tag != 0)nums[i - tag] = nums[i];
            if (nums[i]== val)++tag;
        }
        return len-tag;
    }
};

我用的方法是将!=指定的数往前移,最后返回容器中不等于指定的数的数目

原文地址:https://www.cnblogs.com/csudanli/p/4905132.html