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.
利用map,如果遍历时有重复,那么直接返回true.
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
map<int, int> temp;
for(int i = 0; i < nums.size(); ++i)
{
if(temp.find(nums[i]) != temp.end())
return true;
else
temp[nums[i]] = 1;
}
return false;
}
};