typedefstruct { int key; int value; int used; } HashEntry; typedefstruct { HashEntry\* data; int size; //hashbiao长度 } HashMap; HashMap\* createHashMap(int size) { HashMap\* map = (HashMap\*)malloc(sizeof(HashMap)); map->data = (HashEntry\*)calloc(size, sizeof(HashEntry)); map->size = size; //for (int i = 0; i < size; i++) { // map->data\[i\].used = 0; // 初始化为未使用 //} returnmap; } voidinsert(HashMap\* map, int key, int value) { int hash = abs(value) % map->size; while (map->data\[hash\].used map->data\[hash\].value != 0) { hash = abs(hash + 1) % map->size; } map->data\[hash\].key = key; map->data\[hash\].value = value; map->data\[hash\].used = 1; // 标记为已使用 } int\* twoSum(int\* nums, int numsSize, int target, int\* returnSize) {
HashMap\* map = createHashMap(HashSize); int\* result = (int\*)malloc(2 \* sizeof(int)); \*returnSize = 2; int i; int hash; for (i = 0; i < numsSize; i++) { int complement = target - nums\[i\];
hash = abs(complement) % HashSize; if (map->data\[hash\].used map->data\[hash\].value != 0) { while (map->data\[hash\].value != complement) {