Simple Java O(n) with O(1) efficient


  • 0
    M
    public int[] findErrorNums(int[] nums) {
            int[] out=new int[2];
            
            for(int i=0;i<nums.length;i++){
                int temp=nums[i];
                temp=Math.abs(temp)-1;
                if(nums[temp]<0) out[0]=temp+1;
                else nums[temp]=-nums[temp]; 
            }
            for(int i=0;i<nums.length;i++){
                if(nums[i]>0){
                    out[1]=i+1;
                    break;
                }
            }
            return out;
        }
    

Log in to reply
 

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