当前位置:网站首页>Leetcode 705.设计哈希集合
Leetcode 705.设计哈希集合
2022-07-29 21:24:00 【cwtnice】
原题链接:Leetcode 705. Design HashSet
Design a HashSet without using any built-in hash table libraries.
Implement MyHashSet class:
void add(key)Inserts the value key into the HashSet.bool contains(key)Returns whether the value key exists in the HashSet or not.void remove(key)Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.
Example 1:
Input
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
Output
[null, null, null, true, false, null, true, null, false]
Explanation
MyHashSet myHashSet = new MyHashSet();
myHashSet.add(1); // set = [1]
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(1); // return True
myHashSet.contains(3); // return False, (not found)
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(2); // return True
myHashSet.remove(2); // set = [1]
myHashSet.contains(2); // return False, (already removed)
Constraints:
- 0 <= key <= 106
- At most 104 calls will be made to add, remove, and contains.
方法一:链地址法
思路:
用链地址法来解决冲突
C++代码:
class MyHashSet {
private:
// 用链地址法 同义词在一个链表上
vector<list<int>> hash_table;
// 设置一个质数m
static const int m = 11;
// 哈希函数
static int hash(int key) {
return key % m;
}
public:
MyHashSet(): hash_table(m) {
}
// 插入
void add(int key) {
int h = hash(key);
// 遍历 如果没有就插入
for (auto it = hash_table[h].begin(); it != hash_table[h].end(); it++) {
if ((*it) == key) {
return;
}
}
hash_table[h].push_back(key);
}
// 删除
void remove(int key) {
int h = hash(key);
for (auto it = hash_table[h].begin(); it != hash_table[h].end(); it++) {
if ((*it) == key) {
hash_table[h].erase(it);
return;
}
}
}
// 是否存在
bool contains(int key) {
int h = hash(key);
for (auto it = hash_table[h].begin(); it != hash_table[h].end(); it++) {
if ((*it) == key) {
return true;
}
}
return false;
}
};
/** * Your MyHashSet object will be instantiated and called as such: * MyHashSet* obj = new MyHashSet(); * obj->add(key); * obj->remove(key); * bool param_3 = obj->contains(key); */
边栏推荐
- The Ministry of Human Resources and Social Security announced that "database operation administrator" has become a new occupation, and OceanBase participated in the formulation of occupational standar
- Add a logo to the upper left corner of the picture, add time and address to the lower left corner, and wrap the line when the address reaches the specified length
- 02-SDRAM:自动刷新
- leetcode122. Best Time to Buy and Sell Stock II
- 1. Promise usage in JS, 2. The concept and usage of closures, 3. The difference between the four methods and areas of object creation, 4. How to declare a class
- 南华早报 | 助力亚洲最具公信力报章实现AD域自动化管理
- 惠普服务器硬盘指示灯不亮或显示蓝色
- Numpy array processing (2)
- spyder打不开解决方案
- 品牌广告投放平台的中台化应用与实践
猜你喜欢

leetcode-593:有效的正方形

Verilog 加法器设计

Cobaltstrike and BurpSuite desktop shortcut configuration

GBASE 8s 数据库的恢复

INFTnews | Forbes Web3 exploration

南信大提出TIPCB,一个简单但有效的用于基于文本的人员搜索的基于部分的卷积baseline

官宣!苏州吴江开发区上线电子劳动合同平台

5V升压充电8.4V芯片

The Ministry of Human Resources and Social Security announced that "database operation administrator" has become a new occupation, and OceanBase participated in the formulation of occupational standar

ALBERT: A Lite BERT for Self-supervised Learning of Language Representations
随机推荐
UDP协议详解
全球都热炸了,谷歌服务器已经崩掉了
GBASE 8s 数据库的逻辑日志备份
AI全流程开发难题破解之钥
给图片左上角加logo标识、左下角加时间和地址、地址到达指定长度换行
容器网络硬核技术内幕 (22) 安全与自由兼顾
linux使用脚本安装redis
解决reudx中的异步问题 applyMiddleware thunk
PointPillars 工程复现
容器网络硬核技术内幕 (小结-中)
三品牌下半年将带来多款新品,东风日产将迎来“产品大潮”
【Verilog 设计】Verilog 实现偶数、奇数分频和任意小数分频
网站ping端口的操作方法和命令介绍
Liu Genghong, boys and girls, come here!Sports data analysis and mining!(with a full set of code and data sets)
第3章业务功能开发(线索关联市场活动,动态搜索)
GBASE 8s 如何并行执行update statistics
程序员自由工作的三大痛点?一文教你统统解决
02-SDRAM:自动刷新
APM电机输出逻辑(Motors类详解)
LeetCode--single linked list--146.LRU cache