【easy】27. Remove Element

删除等于n的数,并返回剩余元素个数

Given nums = [3,2,2,3], val = 3,

Your function should return length = 2, with the first two elements of nums being 2.
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int count = 0;
        int len = nums.size();
        
        for (int i=0;i<len;i++){
            if (nums[i]!=val){
                nums[count++] = nums[i]; 
            }
        }
        
        return count;
    }
};

 

posted @ 2018-01-18 22:32  Sherry_Yang  阅读(81)  评论(0编辑  收藏  举报