您的位置:首页 > 大数据 > 人工智能

leetcode Contains Duplicate

2015-11-13 13:13 507 查看
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.

Subscribe to see which companies asked this question

用map做的很简单,不赘述

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
map<int,int> M1;
int temp;
if (nums.empty()) return false;
for(auto i=nums.begin();i!=nums.end();i++){
temp=(*i);
++M1[temp];
}
for(auto j=M1.begin();j!=M1.end();j++){
if((*j).second>1) return true;
}
return false;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: