AC Java O(n) Solution


  • 0
    public int[] findErrorNums(int[] nums) {
            if(nums==null||nums.length==0||nums.length==1) return new int[0];
            int n = nums.length;
            int res [] = new int [2];
            int [] tmp = new int [n];
            for(int num : nums){
                tmp[num-1]++;
            }
    
            for(int i = 0;i<n;i++){
                if(tmp[i]==2){
                    res[0] = i+1;
                }else if(tmp[i]==0){
                    res[1] = i + 1;
                }
            }
            return res;
        }
    

Log in to reply
 

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