My Java solution


  • 0
    W
       public int removeDuplicates(int[] nums) {
            
            if(nums.length<=2) return nums.length;
            int p0=0;
            int p=1;
            int count=1;
            while(p<nums.length) {
    
                if(nums[p]!=nums[p-1]) {
                    count=1;
                } else {
                    count++;
                }
    
                if(count<=2){
                    p0++;                
                    nums[p0]=nums[p];
    
                }
    
                p++;          
            }
            
            return p0+1;
        }

Log in to reply
 

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