纯C手撕leetcode-基本数据结构-hash table

Hash table
纯C实现两数之和和Hashtable

struct listNode {
    int  key;
    int val;
    struct listNode *next;
};

struct HashTable{
    struct listNode **node;
    int size;
};

#define MAXSIZE (10000)
#define HASH(key) ((((key)>0)?(key):(-key)) % (MAXSIZE))

void hashtable_init(struct HashTable *hashTable, int size){

    hashTable->node = malloc(sizeof(struct listNode *) * size);
    hashTable->size = size;
    int i;
    for (i = 0; i < size; i++) {
        hashTable->node[i] = malloc(sizeof(struct listNode ));
        hashTable->node[i]->next = NULL;
    }
}

void hashtable_del_list(struct listNode *head)
{
    struct listNode *cur = head->next;
    while(cur) {
        head->next = cur->next;
        free(cur);
        cur = head->next;
    }

    free(head);
}

void hashtable_exit(struct HashTable *hashTable)
{
    int i;
    for(i=0;isize;i++){
        struct listNode *head = hashTable->node[i];
        hashtable_del_list(head);
    }

    free(hashTable->node);
}

struct listNode *hashtable_find(struct HashTable *hashTable, int key) {
    struct listNode *head = hashTable->node[HASH(key)];
    struct listNode *p = head->next;
    while(p && p->key != key)
        p = p->next;

    return p;
}
//插入k,v 到hash table, hash冲突的通过链表链接到一个链表里
void hashtable_insert(struct HashTable *hashTable, int key, int value) {

    struct listNode *head = hashTable->node[HASH(key)];

    struct listNode *node = malloc(sizeof(struct listNode));
    node->val = value;
    node->key = key;
    node->next = head->next;
    head->next = node;

}

int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    int *arr=malloc(sizeof(int) *2);
    struct HashTable hashtable;
    hashtable_init(&hashtable, MAXSIZE);
    int i;
    for(i=0;ival;
            arr[1] = i;
            break;
        }
        else {
            hashtable_insert(&hashtable, nums[i], i);
        }
    }
    hashtable_exit(&hashtable);
    *returnSize = 2;
    printf("arr[0]:%d, arr[1]:%d\n", arr[0], arr[1]);
    return arr;
}

三数之和
https://leetcode-cn.com/problems/3sum/
思路:

  1. 数据结构:hashtable
  2. 固定i, j, 然后hash 查找-(nums[i]+nums[j]);
  3. 注意:不能重复,需排序,且每个循环,bypass 前后连续的成员
struct listNode {
    int  key;
    int val;
    struct listNode *next;
};

struct HashTable{
    struct listNode **node;
    int size;
};
#define MAXANSSIZE (1000)
#define MAXSIZE (100000)
#define HASH(key) ((((key)>0)?(key):(-key)) % (MAXSIZE))

void hashtable_init(struct HashTable *hashTable, int size){

    hashTable->node = malloc(sizeof(struct listNode *) * size);
    hashTable->size = size;
    int i;
    for (i = 0; i < size; i++) {
        hashTable->node[i] = malloc(sizeof(struct listNode ));
        hashTable->node[i]->next = NULL;
    }
}

void hashtable_del_list(struct listNode *head)
{
    struct listNode *cur = head->next;
    while(cur) {
        head->next = cur->next;
        free(cur);
        cur = head->next;
    }

    free(head);
}

void hashtable_exit(struct HashTable *hashTable)
{
    int i;
    for(i=0;isize;i++){
        struct listNode *head = hashTable->node[i];
        hashtable_del_list(head);
    }

    free(hashTable->node);
}

struct listNode *hashtable_find(struct HashTable *hashTable, int key) {
    struct listNode *head = hashTable->node[HASH(key)];
    struct listNode *p = head->next;
    while(p && p->key != key)
        p = p->next;

    return p;
}
//插入k,v 到hash table, hash冲突的通过链表链接到一个链表里
void hashtable_insert(struct HashTable *hashTable, int key, int value) {

    struct listNode *head = hashTable->node[HASH(key)];

    struct listNode *node = malloc(sizeof(struct listNode));
    node->val = value;
    node->key = key;
    node->next = head->next;
    head->next = node;

}
static int **ans;
static int ansSize;
void ans_insert(int a, int b, int c)
{
    ans[ansSize] = malloc(sizeof(int) * 3);
    ans[ansSize][0] = a;
    ans[ansSize][1] = b;
    ans[ansSize][2] = c;

    ansSize++;
}

void sort(int *nums, int size)
{
    int i, j;
    for(i=0;i<(size - 1); i++)
        for(j=0;j<(size -1 - i);j++)
            if (nums[j] > nums[j+1]) {
                int temp = nums[j];
                nums[j] = nums[j+1];
                nums[j+1] = temp;
            }
}

int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){

    if (numsSize < 3) {
        *returnSize = 0;
        *returnColumnSizes = NULL;
        return NULL;
    }

    ans = malloc(sizeof(int *) * MAXANSSIZE);
    ansSize = 0;
    *returnColumnSizes = malloc(sizeof(int) * MAXANSSIZE);
    struct HashTable hash;
    hashtable_init(&hash, MAXSIZE);
    int i,j;

    for (i = 0; i < MAXANSSIZE; i++)
        (*returnColumnSizes)[i] = 3;

    sort(nums, numsSize);
    //1. insert nums 数组到hash table 中
    for (i = 0; i < numsSize; i++)
            hashtable_insert(&hash, nums[i], i);

    numsSize = i;
    for(i = 0; i< numsSize;i++)
       printf("%d %d\n", i, nums[i]);
    
    //reuturn NULL;
    for(i = 0; i < (numsSize - 2); i++) {
        if (i && nums[i-1] == nums[i])
            continue;
        int target = -nums[i];
        for(j = i+1; j < (numsSize - 1); j++) {

            if (j>(i+1) && nums[j-1] == nums[j])
                continue;
            int find = target - nums[j];
            struct listNode *head = hash.node[HASH(find)];
            struct listNode *p = head->next;
            while (p) {
                if ((p->key == find) && (p->val > j) && (p->val == j+1 || (nums[p->val] != nums[p->val-1])))
                    ans_insert(nums[i], nums[j], nums[p->val]);
                p = p->next;
            }
        }     
    }
    hashtable_exit(&hash);
    *returnSize = ansSize;
    return ans;
}

你可能感兴趣的:(纯C手撕leetcode-基本数据结构-hash table)