寒光博客

[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. take my online stats exam Google Chrome 94.0.4606.81 Windows 10 印度 印度

    Can I hire someone who take my online class for me? You can, yes, and you really should in terms of marketing. Hire a professional to hire the online exams makes too much sense. One point is that time and resources do not need to be spent on completing tasks. Secondly, if you attract someone to ranks that are promised. Online classes are a problem since they are tedious. When your academic and social life gets infective, it is not simple to spend between 10 to 15 hours a day on your course. They take the exam as well as guaranteeing good grades. They recruit the best exam helper from prestigious colleges.

  2. 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 中国 湖北 武汉
  3. Essay assignment help adelaide Google Chrome 94.0.4606.81 Windows 7 荷兰 荷兰

    Essay writing for any subject seems hard task to the college or university students. If the scholars put lots of work in drafting general assignments, they fail to get most excellent grades in the end. Our Essay assignment help adelaide is considered to get rid of the academic writing fears from our customer’s life. We assure you to bring the best-quality assignment help to the college standards.

  4. write my assignment in Australia Google Chrome 94.0.4606.81 Windows 7 荷兰 荷兰

    Students from all around Australia came to us for assignment assistance. Students frequently inquire if we have any tutors available outside of Sydney. Discover write my assignment in Australia where our top online assignment writing service has carved out a place. We've recruited online assignment assistants in several Australian cities. It's why, when students come to us for specialized college assignment assistance in Australia, we know exactly what they're looking for. We find tutors who have already worked on projects from the university where you are enrolled.

  5. Gracia Smith Google Chrome 94.0.4606.81 Windows 10 印度 印度

    Allassignmenthelp.com helps you to stay at the top of the class with assignments online Australia. We provide the finest quality assignment help and that too at very reasonable prices. Nowadays, many options offer high-quality assignment help but we suggest you do not fall for prey. Always review their services before hiring someone. For this, you can read their reviews and look at their sample work that gives you an idea of the quality.

  6. take my online class Safari 605.1.15 Mac OSX 10_15_6 印度 印度

    Just in case you are also filing your Google search box with queries like this, you have landed on the right page. There are few simple things that you should do while you are typing “pay someone to do my homework”. First of all, see whether or not you actually need a helper. If mending your schedule works for you, then try doing that. Once you have set your mind that you require a professional, always check the credibility of a service provider before placing an order. Do not rush into ordering as it can cause you loss as well. Having an expert by your side can be rewarding only when you choose a suitable professional.

  7. office.com/setup Google Chrome 93.0.4577.82 Windows 10 印度 印度

    Amazing website, Love it. Great work done.

  8. Assignment writing help Google Chrome 94.0.4606.61 Windows 10 印度 印度

    Looking for an academic writer to deliver assignment writing help? Visit our website for getting top-quality writing services. Our Ph.D. level professionals supply top-notch projects within your desired time. Browse our website for knowing more details about the services.

  9. essay writer Google Chrome 76.0.3809.132 Windows 10 印度 印度

    Allessaywriter.com is dedicated to offering premium quality essay writer across all genres, including cause and effect, descriptive, comparative, argumentative and more. If you need flawlessly drafted and well-referenced essay writing at the best price, look nowhere else, and count on us.

  10. assignment writing help Google Chrome 76.0.3809.132 Windows 10 印度 印度

    MyAssignmenthelp.com offers you all the necessary support that you may need to solve your pending assignment. Here is a brief description of what you can expect from the best website for assignment writing help on the internet.

  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