【题目概要】
217. Contains Duplicate
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.
Example 1:
Input: [1,2,3,1]
Output: true
Example 2:
Input: [1,2,3,4]
Output: false
【思路分析】
- 线性查找法,时间复杂度太大
- 快排+遍历比较
- Hash表获取冲突即可
【代码示例】
int cmp(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
}
bool containsDuplicate(int* nums, int numsSize){
qsort(nums, numsSize, sizeof(int), cmp);
for(int i=0; i<numsSize-1; i++)
{
if(nums[i] == nums[i+1])
return true;
}
return false;
}