No need to jump between values and keys, just do one pass.


  • 0
    L
    public int[] findErrorNums(int[] nums) {
        int[] ans = new int[2];
        Arrays.sort(nums);
        for(int i = 1; i < nums.length; i++){
            if(nums[i]-nums[i-1] == 0){
                ans[0] = nums[i];
            }
            if(nums[i]-nums[i-1] == 2){
                ans[1] = nums[i-1]+1;
            }
        }
        if(nums[0] != 1){
            ans[1] = 1;
        }
        if(nums[nums.length-1] != nums.length){
            ans[1] = nums.length;
        }
        return ans;
    }

  • 0
    L

    @leetesua

    because I used arrays.sort, the time complexity is O(NlogN)


Log in to reply
 

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