Java AC Solution


  • 0
    A
    public int removeDuplicates(final int[] nums) {
            if (nums.length <= 2) {
                return nums.length;
            }
            
            int writeIndex = 2;
            for (int i = 2; i < nums.length; i++) {
                if (nums[i] != nums[writeIndex-1] || nums[i] != nums[writeIndex-2]) {
                    nums[writeIndex++] = nums[i];
                }
            }
            
            return writeIndex;
        }
    

Log in to reply
 

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