LeetCode217. Contains Duplicate 解题小结

Posted 医生工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode217. Contains Duplicate 解题小结相关的知识,希望对你有一定的参考价值。

题目:

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

这题利用unordered_map来解决应该还是很简单的。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_map<int, int> map;
        for (int i = 0; i < nums.size(); i++){
            if (map.find(nums[i]) != map.end()){
                return true;
            }
            else{
                map[nums[i]] = i;
            }
        }
        return false;
    }
};

 

以上是关于LeetCode217. Contains Duplicate 解题小结的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 217. Contains Duplicate

LeetCode 217 Contains Duplicate

217. Contains Duplicate (leetcode)

leetcode-217-Contains Duplicate

[leetcode]217.Contains Duplicate

LeetCode第217题Contains Duplicate