llllmz

导航

27. 移除元素

像个漏斗一样把元素筛出来就好了。

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int head = 0, curIndex = 0;
        for(; curIndex < nums.size(); ++curIndex){
            if(nums[curIndex] != val){
                nums[head++] = nums[curIndex];
            }
        }
        return head;
    }
};

 

posted on 2024-09-06 21:15  神奇的萝卜丝  阅读(9)  评论(0)    收藏  举报