Easiest solution for your reference


  • 0
    Y

    inspired by problem 27. Remove Element and solve it easiliy.

       if(nums.length <= 2) return nums.length;
        int index = 2;
        for(int i = 2; i<nums.length; i++){
            if(nums[i] != nums[index-2]){
                nums[index++] = nums[i];
            }
        }
        return index;

Log in to reply
 

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