标签:哈希
刚开始还以为是什么异或判重;
后来发现是自己想错了- -;
还是哈希好点。
bool containsDuplicate(int* nums, int numsSize) {
if (numsSize <= 1) return false;
int length = 100007;
if (numsSize < length) length = numsSize;
int * hash = (int*)malloc(sizeof(int) * length);
bool * used = (bool*)malloc(sizeof(bool) * length);
for (int i = 0; i < length; i++) used[i] = false;
for (int i = 0; i < numsSize; i++) {
int p = (nums[i] + numsSize) % numsSize;
while (used[p]) {
if (hash[p] == nums[i]) {
free(hash);
free(used);
return true;
}
p++;
if (p == length) p = 0;
}
hash[p] = nums[i];
used[p] = true;
}
free(hash);
free(used);
return false;
}
标签:哈希
原文地址:http://blog.csdn.net/u012925008/article/details/46003235