Super Easy and Brief Solution


  • 0
    0

    by Huai Chun Shih

    public int removeDuplicates(int[] nums) {
        if(nums.length <= 2)
            return 2;
    
        int index = 0;
        boolean allow = nums[0]==nums[1] ? true : false;
        for(int i=1; i<nums.length; i++){
            
            if(nums[index]!=nums[i]){
                nums[++index]=nums[i];
                allow = true;
            }else if(allow){
                nums[++index]=nums[i];
                allow = false;
            }
        }
     return ++index;   
    }

Log in to reply
 

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