Title 27, removing elements

I, entitled 1

Here Insert Picture DescriptionHere Insert Picture Description

Second, the idea

26 questions with similar ideas, will not delete the numbers move forward.

Specific method is to use a pointer to remove non-numeric string next memory, find the non-deleted digital time, the present value of this position, the pointer plus one. Because the pointer points at the current cycle, the situation does not arise in missing characters at least.

Third, the code

public class T0027 {
    public T0027(){

/*        int[] nums = { 3,2,2,3 };
        System.out.println( removeElement(nums, 3 ) );

        for ( int i : nums )
            System.out.print( i +"\t");*/

        int[] nums = { 0,1,2,2,3,0,4,2 };
        System.out.println( removeElement(nums, 2 ) );

        for ( int i : nums )
            System.out.print( i +"\t");

    }

    public int removeElement(int[] nums, int val) {

        int len = 0;

        for ( int i = 0; i < nums.length; i++ ){
            if ( nums[i] != val ){
                nums[len++] = nums[i];
            }
        }

        return len+1;
    }

    public static void main(String[] args) {
        T0027 t0027 = new T0027();
    }
}

  1. Source: stay button (LeetCode)
    link: https: //leetcode-cn.com/problems/remove-element
    copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source. ↩︎

Published 48 original articles · won praise 1 · views 843

Guess you like

Origin blog.csdn.net/weixin_45980031/article/details/104312605