Java solution O(n)


  • 0
    M

    I don't like the 5-line solution.
    Though the code is almost same for computers, It's not easy to read and understand for people.

        public int removeDuplicates(int[] nums) {
            if(nums==null) return 0;
            int i=0, j=1;
            while(j<nums.length){
                if(nums[i]==nums[j]){
                    j++;
                }else { // nums[i] != nums[j]
                    i++;
                    nums[i] = nums[j]; 
                    j++;
                }
            }
            return i+1;
        }
    

Log in to reply
 

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