LeetCode 645. 错误的集合

//采用数组桶
class Solution {
    public int[] findErrorNums(int[] nums) {
        //定义一个新数组来统计nums数组中元素出现的次数
        int[] count = new int[nums.length + 1];
        //统计每个元素出现的次数
        for(int i : nums){
            count[i]++;
        }
        //定义返回结果集
        int[] res = new int[2];
        //出现0次的,即为缺失元素,出现2次的,即为重复元素
        for(int i = 1; i < count.length;i++){
            if(count[i] == 0){
                res[1] = i;
            }else if(count[i] == 2){
                res[0] = i;
            }
        }
        return res;
    }
}
原文地址:https://www.cnblogs.com/peanut-zh/p/13903118.html