27. 移除元素 - LeetCode

27. 移除元素

题目链接

与26题类似

class Solution {
    public int removeElement(int[] nums, int val) {
        int ans = nums.length;
        int p1 = 0;
        for(int p2 = 0; p2 < nums.length; p2++){
            if(nums[p2] == val){
                ans--;
                continue;
            }
            nums[p1++] = nums[p2];
        }
        return ans;
    }
}
posted @ 2021-02-10 09:19  一天到晚睡觉的鱼  阅读(30)  评论(0)    收藏  举报