题目
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.
分析
判断所给整数序列中有无重复元素。
AC代码
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if (nums.empty())
return false;
unordered_set<int> us;
int len = nums.size();
for (int i = 0; i < len; ++i)
{
if (us.count(nums[i]) != 0)
return true;
else
us.insert(nums[i]);
}
return false;
}
};