寒光博客

[leetcode] 645. 错误的集合
集合 s 包含从 1 到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个数字复制了成了集合里面的另外一个数...
扫描右侧二维码阅读全文
04
2021/07

[leetcode] 645. 错误的集合

集合 s 包含从 1 到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个数字复制了成了集合里面的另外一个数字的值,导致集合 丢失了一个数字 并且 有一个数字重复 。

给定一个数组 nums 代表了集合 S 发生错误后的结果。

请你找出重复出现的整数,再找到丢失的整数,将它们以数组的形式返回。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/set-mismatch

思路问题

hashmap计数 主要用getordefault方法初始化没有key的初始值(比直接if先判断再初始化方便多了)
水题

class Solution {
    public int[] findErrorNums(int[] nums) {
        int ans[] = new int[2], result;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        //从1开始到n
        for (int num = 1; num < nums.length + 1; num++) {
            result = map.getOrDefault(num, 0);
            if (result == 2)
                ans[0] = num;
            else if (result == 0)
                ans[1] = num;
        }
        return ans;
    }

}
本文作者:Author:     文章标题:[leetcode] 645. 错误的集合
本文地址:https://www.dxoca.cn/StudyNotes/394.html       百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
Last modification:July 4th, 2021 at 02:33 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment

12 comments

  1. ceaaviation Google Chrome 105.0.0.0 Windows 10 印度 印度

    CEA Aviation is the one of the first-class DGCA Ground classes in delhi in case you are looking for ground instructions then CEA Aviation is the right location for you.CEA Aviation is sharing the all information approximately the dgca ground training and the way to clear dgca floor examination CEA Aviation offer the all information and the training is given by way of the professionally instructor and and you're satisfied the trainer and the lessons the CEA Aviation provide the additionally online and offline each training,and the aviation is certified aviation

  2. ceaaviation Google Chrome 105.0.0.0 Windows 10 印度 印度

    CEA Aviation is the one of the first-class <a href="https://www.ceaaviation.org/ground-class.php".>DGCA Ground classes in delhi in case you are looking for ground instructions then CEA Aviation is the right location for you.CEA Aviation is sharing the all information approximately the dgca ground training and the way to clear dgca floor examination CEA Aviation offer the all information and the training is given by way of the professionally instructor and and you're satisfied the trainer and the lessons the CEA Aviation provide the additionally online and offline each training,and the aviation is certified aviation

  3. Best IELTS Coaching in Delhi Firefox 104.0 Windows 7 印度 印度

    It’s good to take time when preparing for the IELTS exam. Slow and steady practice is essential at the beginning. Best IELTS Coaching in Delhi. But once test-takers improve it’s important that they condition themselves to take the test under assumed test conditions. Each section of the test is timed, so speed matters. Students should start doing test questions in their own time, then start timing themselves once they get more comfortable doing questions.

  4. brownrhichard12 Google Chrome 96.0.4664.110 Windows 10 澳大利亚 澳大利亚
  5. cell phone repair in Vancouver Google Chrome 94.0.4606.81 Windows 10 巴基斯坦 巴基斯坦

    Great survey, I'm sure you're getting a great response.

    1. dxoca Google Chrome 92.0.4515.159 Windows 10 中国 湖北 武汉
  6. office.com/setup Google Chrome 93.0.4577.82 Windows 10 印度 印度

    Amazing website, Love it. Great work done.

  7. dxkite Google Chrome 91.0.4472.124 Windows 10 中国 广东 深圳

    |´・ω・)ノ 偷偷看看

    1. 寒光 Google Chrome 86.0.4240.198 Windows 10 中国 湖北 武汉
      @dxkite

      抓~~住 让你偷看!!OωO