705. 设计哈希集合-706. 设计哈希映射
705.
不使用任何内建的哈希表库设计一个哈希集合(HashSet)。
实现 MyHashSet 类:
void add(key) 向哈希集合中插入值 key 。
bool contains(key) 返回哈希集合中是否存在这个值 key 。
void remove(key) 将给定值 key 从哈希集合中删除。如果哈希集合中没有这个值,什么也不做。
示例:
输入:
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
输出:
[null, null, null, true, false, null, true, null, false]
解释:
MyHashSet myHashSet = new MyHashSet();
myHashSet.add(1); // set = [1]
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(1); // 返回 True
myHashSet.contains(3); // 返回 False ,(未找到)
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(2); // 返回 True
myHashSet.remove(2); // set = [1]
myHashSet.contains(2); // 返回 False ,(已移除)
简单的用mod来实现hash函数,用链地址法来实现,其中mod为一个质数。
class MyHashSet { private: vector<list<int>> data; static const int mod = 769; int hash(int key) { return key % mod; } public: /** Initialize your data structure here. */ MyHashSet(): data(mod) {} void add(int key) { int num = hash(key); for(auto it = data[num].begin(); it != data[num].end(); it++){ if(*it == key) return; } data[num].push_back(key); } void remove(int key) { int num = hash(key); for(auto it = data[num].begin(); it != data[num].end(); it++){ if(*it == key){ data[num].erase(it); return; } } } /** Returns true if this set contains the specified element */ bool contains(int key) { int num = hash(key); for(auto it = data[num].begin(); it != data[num].end(); it++){ if(*it == key) return true; } return false; } };
自己在写程序的时候在全局中设置了mod = 769,然后在构造函数中想把mod的值给到容器,但是报错了。不知道为什么加了static const 就可以了。
而用int mod = 769却通过不了。
static const 是把变量静态处理了,在构造前初始化。
706.
不使用任何内建的哈希表库设计一个哈希映射(HashMap)。
实现 MyHashMap 类:
MyHashMap() 用空映射初始化对象
void put(int key, int value) 向 HashMap 插入一个键值对 (key, value) 。如果 key 已经存在于映射中,则更新其对应的值 value 。
int get(int key) 返回特定的 key 所映射的 value ;如果映射中不包含 key 的映射,返回 -1 。
void remove(key) 如果映射中存在 key 的映射,则移除 key 和它所对应的 value 。
示例:
输入:
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
输出:
[null, null, null, 1, -1, null, 1, null, -1]
解释:
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // myHashMap 现在为 [[1,1]]
myHashMap.put(2, 2); // myHashMap 现在为 [[1,1], [2,2]]
myHashMap.get(1); // 返回 1 ,myHashMap 现在为 [[1,1], [2,2]]
myHashMap.get(3); // 返回 -1(未找到),myHashMap 现在为 [[1,1], [2,2]]
myHashMap.put(2, 1); // myHashMap 现在为 [[1,1], [2,1]](更新已有的值)
myHashMap.get(2); // 返回 1 ,myHashMap 现在为 [[1,1], [2,1]]
myHashMap.remove(2); // 删除键为 2 的数据,myHashMap 现在为 [[1,1]]
myHashMap.get(2); // 返回 -1(未找到),myHashMap 现在为 [[1,1]]
class MyHashMap { public: vector<list<pair<int,int>>> data; static const int mod = 769; int hash(int key){ return key % mod; } /** Initialize your data structure here. */ MyHashMap(): data(mod) {} /** value will always be non-negative. */ void put(int key, int value) { int temp = hash(key); for(auto it = data[temp].begin(); it != data[temp].end(); it++){ if(it->first == key){ it->second = value; return; } } data[temp].push_back({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) { int temp = hash(key); for(auto it = data[temp].begin(); it != data[temp].end(); it++){ if(it->first == key){ return it->second; } } return -1; } /** Removes the mapping of the specified value key if this map contains a mapping for the key */ void remove(int key) { int temp = hash(key); for(auto it = data[temp].begin(); it != data[temp].end(); it++){ if(it->first == key){ data[temp].erase(it); return ; } } } };
跟705差不多,只不过存储的从key,变化pair.
浙公网安备 33010602011771号