Easy Java 2ms Solution


  • 0
     public int removeDuplicates(int[] nums) {
            int c=1;
            int k=-1;
            
            if(nums.length>=0&&nums.length<3)
              return nums.length;
            
        
            nums[++k]=nums[0];
            for(int i=0,j=i+1;i<nums.length-1||j<nums.length;)
                    {
                        if(nums[i]==nums[j]&&c<2)
                          {
                              nums[++k]=nums[i];
                              c++;
                          }
                          
                   
                          else if(nums[i]!=nums[j] )
                         {   
                                c=1;
                              nums[++k]=nums[j];
                         }
                         i=j;
                         j++;
                    }        
                return k+1;
          
      }
    

Log in to reply
 

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