讨论/《哈希表》 - 存在重复元素/
《哈希表》 - 存在重复元素
共 1 个回复
typedef struct {
    int key;
    UT_hash_handle hh;
}HashTable;

HashTable *head = NULL;
/* 清空哈希表 */
void DeleteNode()
{
    HashTable *s = head;
    while (s != NULL) {
        HashTable *tmp = s->hh.next;
        HASH_DEL(head, s);
        free(s);
        s = tmp;
    }
}

bool containsDuplicate(int* nums, int numsSize){
    if (nums == NULL || numsSize == 0) {
        return false;
    }
    head = NULL;
    for (int i = 0; i < numsSize; i++) {
        HashTable *tmp = NULL;
        /* 查找 */
        HASH_FIND_INT(head, nums + i, tmp);
        if (tmp == NULL) {
            tmp = (HashTable *)malloc(sizeof(HashTable));
            tmp->key = *(nums + i);
            /* 添加 */
            HASH_ADD_INT(head, key, tmp);
            continue;
        }
        /* 清空 */
        DeleteNode();
        return true;
    }
    /* 清空 */
    DeleteNode();
    return false;
}