导航

LeetCode Contains Duplicate

Posted on 2016-03-31 00:58  CSU蛋李  阅读(106)  评论(0编辑  收藏  举报

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.

 

  题目大意:给你一个int型数组,看看这个数组是否包含重复的数字,如果包含返回true,否则返回false

建立一个map即可!

 

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        map<int, int> iiMap;
        for (auto &e : nums)
            {
                if(++iiMap[e]>1)return true;
            }
        return false;
    }
};