haspmap

這裏是引用

typedef struct Node_Class Node;

typedef struct HashMap_Class HashMap;

struct Node_Class {
	int key;
	int value;
	struct Node_Class *next;
};


struct HashMap_Class
{
	int threshold; //桶個數
	struct Node_Class** table; //桶列表
};

/*
 * 初始化HashMap
 * @initialCapacity 初始容量,也就是HashMap中桶的個數,一般爲 (預估最大個數)/0.75f+1
 */
void HashMap_Init(HashMap* map,int initialCapacity) {
	int threshold = 1;
	//自動填充爲大於initialCapacity的2的倍數值
	while (threshold < initialCapacity)
		threshold <<= 1;
	map->threshold = threshold;
	Node** table = malloc(sizeof(Node)*threshold);
	memset(table, 0, sizeof(Node) * threshold);
	map->table = table;
}

/*
 * HashMap的Hash值計算,暫時是就是自己
 * 
 */
int HashMap_HashCode(HashMap* map, int key) {
	return key;
}

/*
 * HashMap的put方法,首先將產生的Hash值做高低位異或,然後再取餘,產生的值小於threshold
 * 產生的值作爲桶的下標,找到對應的桶
 * 遍歷桶中的鏈表,一個個比較key值,key相同則替換value值,如果沒有key就在桶最後面加上一個Node
 *
 */
void HashMap_Put(HashMap* map,int key,int value) {
	Node *p;
	Node *tail;
	unsigned int hash = (unsigned int)HashMap_HashCode(map, key);
	//得到Hash值後將高低位交互運算下
	hash= hash ^ (hash >> 16);
	//最後取餘數
	hash=hash&(map->threshold - 1);
	p = map->table[hash];
	//如果桶是空的就新建Node放入桶中
	if (p == NULL) {
		Node* node = malloc(sizeof(Node));
		node->key = key;
		node->value = value;
		node->next = NULL;
		map->table[hash] = node;
	}else {
		while (p != NULL) {
			//如果找到key就替換
			if (p->key == key) {
				p->value = value;
				break;
			}
			tail = p;
			p = p->next;
			//找到末尾也找不到key,就新建Node添加到最後
			if (p == NULL) {
				Node* node = malloc(sizeof(Node));
				node->key = key;
				node->value = value;
				node->next = NULL;
				tail->next = node;
			}
		}
	}
}

/*
 * HashMap的get方法,與put方法差不多,只是找到對應的key之後直接返回Node,其他時候返回NULL
 */
Node* HashMap_Get(HashMap* map, int key) {
	Node *p;
	unsigned int hash = (unsigned int)HashMap_HashCode(map, key);
	hash = hash ^ (hash >> 16);
	hash = hash & (map->threshold - 1);
	p = map->table[hash];
	if (p == NULL) {
		return NULL;
	}
	else {
		while (p != NULL) {
			if (p->key == key) {
				return p;
				break;
			}
			p = p->next;
			if (p == NULL) {
				return NULL;
			}
		}
	}
	return NULL;
}

bool HashMap_Contains(HashMap* map, int key) {
	if (HashMap_Get(map, key) != NULL) {
		return true;
	}
	else {
		return false;
	}
}

void HashMap_Free(HashMap* map) {
	for (int i = 0; i < map->threshold; i++) {
		Node *e***y = map->table[i];
		while (e***y  != NULL) {
			Node *temp = e***y ;
			e***y = e***y ->next;
			free(temp);
		}
	}
	free(map->table);
	free(map);
}

int* twoSum(int* nums, int numsSize, int target)
{
	if (numsSize < 2) return NULL;
	int *res = (int *)malloc(sizeof(int) * 2);
	//最好算一下初始容量,提高效率
	int initialCapacity = (int)((float)numsSize/ 0.75F + 1.0F);
	HashMap* map = malloc(sizeof(HashMap));
	HashMap_Init(map,initialCapacity);
	for (int i = 0; i < numsSize; i++) {
		//在map中尋找餘數
		if (!HashMap_Contains(map,target - nums[i])) {
			HashMap_Put(map,nums[i], i);
		}
		else {
			res[0] = HashMap_Get(map,target - nums[i])->value;
			res[1] = i;
			break;
		}
	}
	HashMap_Free(map);
	return res;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章