Leetcode beginner - removing elements

topic:

This question and in fact before the "delete duplicate numbers" very similar

And this question is easier than the previous title

Since we are still traversing forward, there is need to remove the digital experience, we will move forward the elements behind

Code:

class Solution {
    public int removeElement(int[] nums, int val) {
        int count=0;
        int cur=nums.length-1;
        int i=0;
        while(cur>=0){
            if(nums[cur]==val) {
                int temp = cur;
                count++;
                while (temp < nums.length - 1) {
                    nums[temp] = nums[temp + 1];
                    temp++;
                }
            }
            cur--;
        }
        return nums.length-count;
    }
}

operation result:

Published 25 original articles · won praise 3 · Views 415

Guess you like

Origin blog.csdn.net/qq_39377543/article/details/104088537