美文网首页快乐写代码
T645、错误的集合

T645、错误的集合

作者: 上行彩虹人 | 来源:发表于2020-08-25 21:44 被阅读0次

集合 S 包含从1到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复。
给定一个数组 nums 代表了集合 S 发生错误后的结果。你的任务是首先寻找到重复出现的整数,再找到丢失的整数,将它们以数组的形式返回。
示例 1:
输入: nums = [1,2,2,4]
输出: [2,3]
注意:
给定数组的长度范围是 [2, 10000]。
给定的数组是无序的。

暴力求解

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

相关文章

  • T645、错误的集合

    集合 S 包含从1到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的...

  • 错误集合

    Commit failed with error: did not match any file(s) known...

  • 错误集合

    1.dyld: Library not loaded: @rpath/libswiftCore.dylib R...

  • 错误集合

    1 我的代码错误原因

  • 错误集合

    CocoaPods报错:The dependency AFNetworking is not used in an...

  • 错误集合

    错误一:英文描述 中文描述:大体意思就是这个App缺少一个获取私有(敏感)数据的权限描述,需要我们在info.pl...

  • 错误集合

    1、composer 报 Failed to decode response: zlib_decode(): da...

  • 错误集合

    spring boot 解决后台返回 json 到前台出现中文乱码的问题 微信 登录 Scope 参数错误或没有 ...

  • 错误集合

    MAC,IDEA运行 Test 发生错误 解决办法:1、删除缓存 rm -rf ~/Library/Caches/...

  • 友盟错误

    一 友盟分享错误集合 1 报.o的错误 解决方案 二 友盟分析错误集合 1 更新pods出现冲突错误 解决方案 先...

网友评论

    本文标题:T645、错误的集合

    本文链接:https://www.haomeiwen.com/subject/crhtsktx.html