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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值