Java beat 94% in 8 lines


  • 2

    It is kind of two pointers, but in a concise way

        public int removeDuplicates(int[] nums) {
            if (nums == null || nums.length == 0) {return 0;}
            int len = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] != nums[len]) {
                    nums[++len] = nums[i];
                }
            }
            return len + 1;
        }
    

Log in to reply
 

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