Simple Java solution, easy to understand


  • 0
    Y
     public int removeElement(int[] nums, int val) {
        if(nums==null) return 0;
        int i=0,j=nums.length-1;
        for(;i<=j;){
            if(nums[j]==val){
                j--;
                continue;
            }
            if(nums[i]==val){
                nums[i]=nums[j];
                j--;
            }
            i++;
        }
        return j+1;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.