# LeetCode 706：Design HashMap 实现一个简单的哈希映射

## 题目描述：

Design a HashMap without using any built-in hash table libraries.

To be specific, your design should include these functions:

• put(key, value): Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.
• get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
• remove(key) : Remove the mapping for the value key if this map contains the mapping for the key.

### Example:

MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
hashMap.get(1);            // returns 1
hashMap.put(2, 1);          // update the existing value
hashMap.get(2);            // returns 1
hashMap.remove(2);          // remove the mapping for 2


### Note:

All keys and values will be in the range of [0, 1000000].
The number of operations will be in the range of [1, 10000].
Please do not use the built-in HashMap library.

## 题解

### 实现：

class HashTable:

# 初始化
def __init__(self, size):
self.elem = [None for i in range(size)]                # 创建一个列表保存哈希元素,值全为None
self.count = size                                      # 最大表长

# 散列函数
def hash(self, key):
return key % self.count                                # 散列函数采用除留余数法

# 插入
def insert_hash(self, key):
print(key)
while self.elem[address] != None:                      # 发生冲突
print(self.elem)

# 查找
def search_hash(self, key):
star = address = self.hash(key)                               # 查找关键字
return False
return True

if __name__ == '__main__':
list_a = [0, 12, 67, 56, 16, 25, 37, 22, 29, 15, 47, 48, 34]
hash_table = HashTable(len(list_a))
for i in list_a:
hash_table.insert_hash(i)

for i in hash_table.elem:
if i:
print((i, hash_table.elem.index(i)))

print(hash_table.search_hash(15))
print(hash_table.search_hash(33))



class MyHashMap:
def __init__(self):
"""
"""
self.buckets = 1000                       # 键值块，哈希桶
self.itemsPerBuckect = 1001               # 产生冲突的“拉链”块
self.hashmap = [[] for _ in range(self.buckets)]        # _表示临时变量，仅用一次，后面无需用到

# 散列函数
def hash(self, key):
return key % self.buckets                 # 取余

# 处理冲突的函数
def pos(self, key):
return key // self.buckets                # 向下取整，返回商的整数部分

def put(self, key, value):
"""
value will always be positive.
:type key: int
:type value: int
:rtype: void
"""
hashkey = self.hash(key)
if not self.hashmap[hashkey]:                 # 没有产生冲突，直接填入buckets中
self.hashmap[hashkey] = [None] * self.itemsPerBuckect
self.hashmap[hashkey][self.pos(key)] = value

def get(self, key):
"""
Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
:type key: int
:rtype: int
"""
hashkey = self.hash(key)
if(not self.hashmap[hashkey]) or self.hashmap[hashkey][self.pos(key)] == None:      # 没有找到这个值
return -1
else:
return self.hashmap[hashkey][self.pos(key)]

def remove(self, key):
"""
Removes the mapping of the specified value key if this map contains a mapping for the key
:type key: int
:rtype: void
"""
hashkey = self.hash(key)
if self.hashmap[hashkey]:
self.hashmap[hashkey][self.pos(key)] = None

if __name__ == "__main__":
hashmap = MyHashMap()
hashmap.put(1, 1)
hashmap.put(2, 2)
hashmap.get(1)
hashmap.get(3)
hashmap.put(2, 1)
hashmap.get(2)
hashmap.remove(2)
hashmap.get(2)


python极简版本：

class MyHashMap:

def __init__(self):
self.arr = [-1] * 1000001

def put(self, key, value):
self.arr[key] = value

def get(self, key):
return self.arr[key]

def remove(self, key):
self.arr[key] = -1


c++实现版本，直接用vector容器模拟，没有考虑冲突情况。

class MyHashMap {
public:
/** Initialize your data structure here. */
vector<int> hashMap;
MyHashMap() {

}

/** value will always be non-negative. */
void put(int key, int value) {
if(key >= hashMap.size()){
for(int i = hashMap.size(); i <= key; i++){
hashMap.push_back(-1);
}
}
hashMap[key] = value;
}

/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
int get(int key) {
if(key >= hashMap.size())
return -1;
return hashMap[key];

}

/** Removes the mapping of the specified value key if this map contains a mapping for the key */
void remove(int key) {
if(key >= hashMap.size())
return;
hashMap[key] = -1;
}
};