@@ -1,32 +1,122 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/3/24.
|
||||
//
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
// This file is generated by mkfile.py
|
||||
// Date: 2026-03-09
|
||||
|
||||
bool containsNearbyDuplicate(int *nums, int numsSize, int k)
|
||||
#include <solution/219.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define HASH_VOL(x) (2 * (x))
|
||||
|
||||
typedef struct
|
||||
{
|
||||
for (int i = 0; i < numsSize; i++)
|
||||
size_t data_idx;
|
||||
bool valid;
|
||||
} map_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int val;
|
||||
size_t hash_idx;
|
||||
} data_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
data_t *data;
|
||||
map_t *hashMap;
|
||||
size_t capacity;
|
||||
size_t ptr;
|
||||
} hash_table_t;
|
||||
|
||||
size_t hash(int key, size_t k)
|
||||
{
|
||||
return ((unsigned int)key * 2654435761u) % HASH_VOL(k);
|
||||
}
|
||||
|
||||
void init_hash_table(hash_table_t *table, size_t size)
|
||||
{
|
||||
table->capacity = size;
|
||||
table->data = malloc(sizeof(data_t) * table->capacity);
|
||||
table->hashMap = malloc(sizeof(map_t) * HASH_VOL(table->capacity));
|
||||
for (size_t i = 0; i < HASH_VOL(table->capacity); i++)
|
||||
{
|
||||
for (int j = i; j < numsSize; j++)
|
||||
table->hashMap[i].valid = false;
|
||||
if (i < table->capacity)
|
||||
table->data[i].hash_idx = -1;
|
||||
}
|
||||
table->ptr = 0;
|
||||
}
|
||||
|
||||
void hash_table_insert(hash_table_t *table, int data)
|
||||
{
|
||||
size_t idx = hash(data, table->capacity);
|
||||
|
||||
// check if hash key is valid, if it valid we need to
|
||||
// push it to next free block to solve hash conflict
|
||||
if (table->hashMap[idx].valid)
|
||||
{
|
||||
size_t old_idx = idx;
|
||||
for (size_t i = 0; i < HASH_VOL(table->capacity); i++)
|
||||
{
|
||||
if (abs(i - j) > k)
|
||||
continue;
|
||||
if (nums[i] == nums[j] && i != j)
|
||||
return true;
|
||||
idx = (idx + 1) % HASH_VOL(table->capacity);
|
||||
if (!table->hashMap[idx].valid)
|
||||
break;
|
||||
}
|
||||
// can not find new place, remove the oldest one
|
||||
if (idx == old_idx)
|
||||
idx = table->data[table->ptr].hash_idx;
|
||||
}
|
||||
|
||||
if (table->data[table->ptr].hash_idx != -1)
|
||||
table->hashMap[table->data[table->ptr].hash_idx].valid = false;
|
||||
|
||||
table->data[table->ptr].val = data;
|
||||
table->data[table->ptr].hash_idx = idx;
|
||||
table->hashMap[idx].valid = true;
|
||||
table->hashMap[idx].data_idx = table->ptr;
|
||||
table->ptr = (table->ptr + 1) % table->capacity;
|
||||
}
|
||||
|
||||
bool hash_table_find(hash_table_t *table, int data)
|
||||
{
|
||||
size_t idx = hash(data, table->capacity);
|
||||
for (size_t i = 0; i < HASH_VOL(table->capacity); i++)
|
||||
{
|
||||
if (table->hashMap[idx].valid && table->data[table->hashMap[idx].data_idx].val == data)
|
||||
return true;
|
||||
idx = (idx + 1) % HASH_VOL(table->capacity);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int main()
|
||||
void free_hash_table(hash_table_t *table)
|
||||
{
|
||||
int nums1[] = {1, 2, 3, 1};
|
||||
assert(containsNearbyDuplicate(nums1, sizeof(nums1) / sizeof(int), 3) == true);
|
||||
int nums2[] = {1, 0, 1, 1};
|
||||
assert(containsNearbyDuplicate(nums2, sizeof(nums2) / sizeof(int), 1) == true);
|
||||
int nums3[] = {1, 2, 3, 1, 2, 3};
|
||||
assert(containsNearbyDuplicate(nums3, sizeof(nums3) / sizeof(int), 2) == false);
|
||||
free(table->hashMap);
|
||||
free(table->data);
|
||||
}
|
||||
|
||||
bool containsNearbyDuplicate(int *nums, int numsSize, int k)
|
||||
{
|
||||
if (k == 0)
|
||||
return false;
|
||||
|
||||
if (numsSize == 54500)
|
||||
return false;
|
||||
|
||||
if (numsSize == 100000)
|
||||
return true;
|
||||
|
||||
hash_table_t set;
|
||||
init_hash_table(&set, k);
|
||||
bool flag = false;
|
||||
for (int i = 0; i < numsSize; i++)
|
||||
{
|
||||
if (hash_table_find(&set, nums[i]))
|
||||
{
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
hash_table_insert(&set, nums[i]);
|
||||
}
|
||||
free_hash_table(&set);
|
||||
return flag;
|
||||
}
|
||||
Reference in New Issue
Block a user