寒光博客

[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://dxoca.cn/StudyNotes/394.html       百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
Last modification:July 4th, 2021 at 02:33 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment

15 comments

  1. Software training institute in laxmi nagar Firefox 99.0 Windows 7 印度 印度

    Ordering, Receipting, Rejections, Discounts, and other Purchase Processes ranging Software training institute in laxmi nagar from simple (cash purchase) to advanced (credit card purchase). From the Order to the Receipt (Sales Processes).Orders Received, Delivery, Invoicing, Rejections and Receipting, and POS Invoicing at Retail are all examples of simple (cash sales) to advanced sales processes.

    http://biitnewdelhi.com/

  2. Australia Assignment Help Google Chrome 100.0.4896.75 Windows 7 印度 印度

    If you have no time Use Australia Assignment Help Services for assistance. Writing assignments is difficult, and mostly people find it exhausting. Connect with our assignment helper
    https://greatassignmenthelper.com/au/

  3. Best digital marketing institute in laxmi nagar Firefox 97.0 Windows 7 印度 印度

    BIIT is an IT institution for students who want to achieve academic and professional goals. At BIIT, we understand the individual objectives of students and adapt course content according to that. Since its inception we have been recongised Best tally institute in laxmi nagar and our students have consistently shared their experience through their reviews as excellent.
    http://biitnewdelhi.com/

  4. Assignment Help Google Chrome 98.0.4758.102 Windows 10 印度 印度

    Assignment Help from GreatAssignmentHelper.com offer you flawless uae assignment help compositions that assure best grades at very moderate and affordable rates. Our Assignment Help UAE has been consistently offering precise and excellent Online Assignment Help within assigned timelines to a great many understudies across the UAE.
    https://greatassignmenthelper.com/ae/

  5. Commercial Pilot Training in Canada Google Chrome 98.0.4758.102 Windows 7 印度 印度

    CEA Aviation Pilot Training is an important part of learning. Teachers assign pilot training to their students so that they study what they have learned and try some questions or activity to show their understanding of the topic. Commercial Pilot Training in Canada.There are a lot of students who do not like to do Pilot Training.

    http://ceaaviation.org/canada.php

  6. brownrhichard12 Google Chrome 96.0.4664.110 Windows 10 澳大利亚 澳大利亚
  7. assignment writing services Firefox 94.0 Windows 7 印度 印度

    There is an assignment service that also provides assistance with assignment writing services. It is one thing to pen down all your skills and knowledge and another to do it in a printed way. There is a lot of assignment writing especially in the USA but look for the one who can provide you the best quality service, who has experience in this field, who has already written a lot of successful assignments. Also, try to make a consultation call before anyone considers the best assignment writing service.
    https://www.allassignmenthelp.com/assignment-writing-service.html

  8. hillgibson Google Chrome 95.0.4638.69 Windows 10 印度 印度

    Have you ever questioned if someone can "pay to take my online class?" Or "take my online class?" You are not alone if the answer is 'yes,' because many students are struggling or stuck in an online class. This is a reality now and service providers can provide professionals for your online courses and exam. Online courses can look like an easier option. But such online courses have the pressure to balance academics and professional work. Most people who take online classes often end up missing courses or are late with homework assignments.
    https://www.takemyonlineclassesforme.com/
    https://www.takemyonlineclassesforme.com/take-my-online-exam.html
    https://www.takemyonlineclassesforme.com/take-my-online-english-class.html
    https://www.takemyonlineclassesforme.com/pay-someone-to-take-my-statistics-exam.html

  9. 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 中国 湖北 武汉
  10. office.com/setup Google Chrome 93.0.4577.82 Windows 10 印度 印度

    Amazing website, Love it. Great work done.

  11. Do my homework Google Chrome 93.0.4577.82 Windows 10 印度 印度

    Produce a construction to shows your free reference generator assignments logically. This may aid you a full heap of faces around the issue and conjointly typically doesn't set off this space. Even as the niche can presently be time tested instead of being useful for you in the person. Once developing a well-suited draft on the mission, update this, and make certain it's cheap pay to do my homework online and contains everything at your Allassignmenthelp. https://www.allassignmenthelp.com/pay-someone-to-do-my-homework.html

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

    |´・ω・)ノ 偷偷看看

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

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

      1. Assignment helpers Google Chrome 98.0.4758.102 Windows 10 印度 印度
        @寒光

        Hard times and hard tasks, the assignments, and the throttling deadliness. We all are aware of such stifling occasions from our university days. What many of us didn’t know is that we may get it done by paying a small fee to an Assignment Helpers. I opted for the service of greatassignementhelper.com and I think that you should try it as well.