217. Contains Duplicate

Posted Lee

tags:

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

Problem:

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.

 

A possible solution:

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        map<int,int> int_map;
        for(int i=0;i<nums.size();i++)
        {
            if(int_map.count(nums[i]))
                return true;
            int_map.insert(pair<int,int>(nums[i],i));
        }
        return false;
    }
};

  

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

LeetCode_217. Contains Duplicate

217. Contains Duplicate

LeetCode 217. Contains Duplicate

217 Contains Duplicate

LeetCode 217 Contains Duplicate

217. Contains Duplicate (leetcode)