解法一:排序方法:O(nlogn)
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if (nums.size() <=1)
return false;
sort(nums.begin(), nums.end());
for (int i = 0; i< nums.size(); i++)
{
if (nums[i] == nums[i+1])
return true;
}
return false;
}
};
解法二:哈希表 O(n)
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if (nums.empty())
return false;
unordered_map<int,int> mp;
for (int i : nums)
{
if (++mp[i]>1)
return true;
}
return false;
}
};